概要
要實現Restful風格,主要有兩個方面要講解,如下:
1. 同一個資源,如果需要返回不同的形式,如:json、xml等;
不推薦的做法:
- /user/getUserJson
- /user/getUserXML
這樣做不符合Restful的原則,1個資源相當於變成了兩個資源;
2. 對同一資源的CRUD操作
不推薦的做法:
- /user/addUser/
- /user/getUser/123
- /user/deleteUser/123
- /user/updateUser/123
這樣做也不符合Restful的原則,1個資源相當於變成了多個資源;
本篇文章將介紹《 同一個資源,如果需要返回不同的形式,如:json、xml等;》如何實現。
至於《對同一資源的CRUD操作》,將留在下一篇文件講解。
內容協商介紹
RESTful服務中很重要的一個特性即是同一資源,多種表述,也即如下面描述的三種方式:
- 方式1:使用http request header: Accept
- GET /user/123 HTTP/1.1
- Accept: application/xml //將返回xml格式數據
- GET /user/123 HTTP/1.1
- Accept: application/json //將返回json格式數據
- 方式2:使用擴展名
- /user/123.xml 將返回xml格式數據
- /user/123.json 將返回json格式數據
- /user/123.html 將返回html格式數據
- 方式3:使用參數
- /user/123?format=xml //將返回xml數據
- /user/123?format=json //將返回json數據
以上三種優缺點比較
- 使用Accept header ==>由於瀏覽器的差異,一般不使用此種方式
這一種為教科書中通常描述的一種,理想中這種方式也是最好。但由於瀏覽器的差異,發送上來的Accept Header頭是不一樣的。 將導致服務器不知要返回什么格式的數據給你。 下面是瀏覽器的Accept Header
- chrome:
- Accept:application/xml,application/xhtml+xml,textml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
- firefox:
- Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
- 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, */*
- 使用擴展名
喪失了同一url多種展現的方式,但現在這種在實際環境中是使用最多的,因為更加直觀,方便。
- 使用參數
現在很多open API是使用這種方式,但因為要編寫的字符較多,所以不如“使用擴展名”使用的多。
使用ContentNegotiatingViewResolver內容協商視圖解析器
ContentNegotiatingViewResolver可用於實現上述三種方式的Restful的風格,因為使用Accept不是很推薦,所以我們的配置使用了“使用擴展名”和“使用參數”這兩種方式。下面直接看下配置吧:



我們在A處配置的是JSON視圖對象,而B處配置的是XML視圖對象,他們都是默認的候選視圖對象,會覆蓋對應的視圖解析器返回的視圖對象。簡單的說,就是:
1. 如果資源的URL形如:
- /user/123.json 或
- /user/123?format=json
則直接返回A處對應的視圖對象,即JSON格式的視圖對象;
2. 如果資源的URL形如:
- /user/123.xml
- /user/123?format=xml
則直接返回B處對應的視圖對象,即XML格式的視圖對象;
有一點需要說明,
xml解析需要的jar包:
- xpp3_min-xxx.jar;
- xstream-xxx.jar。
3. 如果資源的URL形如:
- /user/123.html
- /user/123?format=html
則由C處對應的
InternalResourceViewResolver
視圖解析器解析;
4. 如果資源的URL不帶任何參數,形如:
- /user/123
則由C處對應的
InternalResourceViewResolver
視圖解析器解析,因為已經默認制定
p:defaultContentType
="text/html"
;
配置文件的完整代碼如下:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 掃描web包,應用Spring的注解 -->
<context:component-scan base-package="com.ll.web"/>
<mvc:annotation-driven/>
<!-- 協商多種視圖解析器 -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"
p:order="0"
p:ignoreAcceptHeader="true"
p:favorPathExtension="true"
p:favorParameter="true"
p:parameterName="format"
p:defaultContentType="text/html">
<!-- 用來定義哪些擴展名(如:/user/123.json),或協商參數值(如:/user/123?format=xml)是可識別的 -->
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="xml" value="application/xml" />
<entry key="json" value="application/json" />
</map>
</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>
<!-- Excel及PDF視圖解析器配置 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"
p:order="10" />
<!-- 配置視圖解析器,將ModelAndView及字符串解析為具體的頁面,默認優先級最低 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:order="100"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/resource/jsp/"
p:suffix=".jsp" />
<!-- spring mvc對靜態資源的訪問 -->
<mvc:resources mapping="/resource/**" location="/resource/**" />
</beans>
控制層代碼

測試
1. json格式


2. xml格式




4. 不帶任何參數
http://localhost:8080/SpringMVCTest/test/list.xxx (不是上述提到的后綴名)
或




其他
博客:
淘寶-代做畢設:
附件列表