接上一篇对spring rest的描述.
一.REST内容协商介绍 
RESTful服务中很重要的一个特性即是同一资源,多种表述.也即如下面描述的三种方式: 
1.使用http request header: Accept
 GET /user/123 HTTP/1.1
GET /user/123 HTTP/1.1
 Accept: application/xml                 //将返回xml格式数据
Accept: application/xml                 //将返回xml格式数据

 GET /user/123 HTTP/1.1
GET /user/123 HTTP/1.1
 Accept: application/json               //将返回json格式数据
Accept: application/json               //将返回json格式数据 /user/123.xml  将返回xml格式数据
/user/123.xml  将返回xml格式数据
 /user/123.json 将返回json格式数据
/user/123.json 将返回json格式数据
 /user/123.html 将返回html格式数据
/user/123.html 将返回html格式数据/user/123?format=xml          //将返回xml数据
/user/123?format=json          //将返回json数据
而以上三种各有优缺点:
1.使用Accept header:
   这一种为教科书中通常描述的一种,理想中这种方式也是最好的,但如果你的资源要给用户直接通过浏览器访问(即html展现),那么由于浏览器的差异,发送上来的Accept Header头将是不一样的. 将导致服务器不知要返回什么格式的数据给你. 下面是浏览器的Accept Header
 chrome:
chrome:   
 Accept:application/xml,application/xhtml+xml,textml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept:application/xml,application/xhtml+xml,textml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5   
 
  
 firefox:
firefox:   
 Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8   
 
  
 IE8:
IE8:   
 Accept:image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*
Accept:image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */* 2.使用扩展名
  丧失了同一url多种展现的方式,但现在这种在实际环境中是使用最多的.因为更加符合程序员的审美观.
3.使用参数
  可能由于要编写的字符较多,所以较少使用.
带着上面的选择: 使用扩展名,我们来看一下spring中如何配置这部分.
二.spring rest配置
现spring完成内容协商(content negotiation)的工作是由ContentNegotiatingViewResolver来完成的.它的工作模式支持我上面讲的三种, 
ContentNegotiatingViewResolver是根据客户提交的MimeType(如 text/html,application/xml)来跟服务端的一组viewResover的MimeType相比较,如果符合,即返回viewResover的数据.
而 /user/123.xml, ContentNegotiatingViewResolver会首先将 .xml 根据mediaTypes属性将其转换成 application/xml,然后完成前面所说的比较.
下面是ContentNegotiatingViewResolver的完全配置.
    <!-- 根据客户端的不同的请求决定不同的view进行响应, 如 /blog/1.json /blog/1.xml -->
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
                <!-- 设置为true以忽略对Accept Header的支持-->
                <property name="ignoreAcceptHeader" value="true"/>
                <!-- 在没有扩展名时即: "/user/1" 时的默认展现形式 -->
        <property name="defaultContentType" value="text/html"/>
        
                <!-- 扩展名至mimeType的映射,即 /user.json => application/json -->
        <property name="mediaTypes">
            <map>
                <entry key="json" value="application/json" />
                <entry key="xml" value="application/xml" />
            </map>
        </property>
        <!-- 用于开启 /userinfo/123?format=json 的支持 -->
        <property name="favorParameter" value="false"/>
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
                <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
                    <property name="prefix" value="/pages"/>
                    <property name="suffix" value=".jsp"></property>
                </bean>
            </list>
        </property>
        <property name="defaultViews">
            <list>
                <!-- for application/json -->
                <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
                <!-- for application/xml -->
                <!-- 
                <bean class="org.springframework.web.servlet.view.xml.MarshallingView" >
                    <property name="marshaller">
                        <bean class="org.springframework.oxm.xstream.XStreamMarshaller"/>
                    </property>
                </bean>
                 -->
            </list>
        </property>
    </bean>
查看demo:  
http://demo.rapid-framework.org.cn:8080/springmvc_rest_demo/userinfo
demo下载:  
http://rapid-framework.googlecode.com/files/springmvc_rest_demo.zip