【Spring學習筆記-MVC-18.1】Spring MVC實現RESTful風格-同一資源,多種展現:xml-json-html


概要


要實現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
  1. GET /user/123 HTTP/1.1  
  2. Accept: application/xml                 //將返回xml格式數據  
  3.   
  4. GET /user/123 HTTP/1.1  
  5. Accept: application/json               //將返回json格式數據  
  • 方式2:使用擴展名
  1. /user/123.xml  將返回xml格式數據  
  2. /user/123.json 將返回json格式數據  
  3. /user/123.html 將返回html格式數據 

  • 方式3:使用參數
  1. /user/123?format=xml          //將返回xml數據  
  2. /user/123?format=json          //將返回json數據  

以上三種優缺點比較

  • 使用Accept header   ==>由於瀏覽器的差異,一般不使用此種方式
 這一種為教科書中通常描述的一種,理想中這種方式也是最好。但由於瀏覽器的差異,發送上來的Accept Header頭是不一樣的。 將導致服務器不知要返回什么格式的數據給你。 下面是瀏覽器的Accept Header
  1. chrome:  
  2. Accept:application/xml,application/xhtml+xml,textml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5  
  3.   
  4. firefox:  
  5. Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8  
  6.   
  7. IE8:  
  8. 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包:
  1. xpp3_min-xxx.jar;
  2. xstream-xxx.jar。

3. 如果資源的URL形如:
  • /user/123.html 
  • /user/123?format=html 
則由C處對應的 InternalResourceViewResolver 視圖解析器解析;
4. 如果資源的URL不帶任何參數,形如:
  • /user/123
則由C處對應的 InternalResourceViewResolver 視圖解析器解析,因為已經默認制定  p:defaultContentType ="text/html"

配置文件的完整代碼如下:

    
    
    
            
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  12. <!-- 掃描web包,應用Spring的注解 -->
  13. <context:component-scan base-package="com.ll.web"/>
  14. <mvc:annotation-driven/>
  15. <!-- 協商多種視圖解析器 -->
  16. <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"
  17. p:order="0"
  18. p:ignoreAcceptHeader="true"
  19. p:favorPathExtension="true"
  20. p:favorParameter="true"
  21. p:parameterName="format"
  22. p:defaultContentType="text/html">
  23. <!-- 用來定義哪些擴展名(如:/user/123.json),或協商參數值(如:/user/123?format=xml)是可識別的 -->
  24. <property name="mediaTypes">
  25. <map>
  26. <entry key="html" value="text/html" />
  27. <entry key="xml" value="application/xml" />
  28. <entry key="json" value="application/json" />
  29. </map>
  30. </property>
  31. <property name="defaultViews">
  32. <list>
  33. <!-- for application/json -->
  34. <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
  35. <!-- for application/xml -->
  36. <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
  37. <property name="marshaller">
  38. <bean class="org.springframework.oxm.xstream.XStreamMarshaller"/>
  39. </property>
  40. </bean>
  41. </list>
  42. </property>
  43. </bean>
  44. <!-- Excel及PDF視圖解析器配置 -->
  45. <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"
  46. p:order="10" />
  47. <!-- 配置視圖解析器,將ModelAndView及字符串解析為具體的頁面,默認優先級最低 -->
  48. <bean
  49. class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  50. p:order="100"
  51. p:viewClass="org.springframework.web.servlet.view.JstlView"
  52. p:prefix="/resource/jsp/"
  53. p:suffix=".jsp" />
  54. <!-- spring mvc對靜態資源的訪問 -->
  55. <mvc:resources mapping="/resource/**" location="/resource/**" />
  56. </beans>


控制層代碼





測試


1. json格式





4. 不帶任何參數

http://localhost:8080/SpringMVCTest/test/list.xxx  (不是上述提到的后綴名) 
  http://localhost:8080/SpringMVCTest/test/list?format=xxx     (不是上述提到的后綴名)

 

其他


博客:
  淘寶-代做畢設:






附件列表

     


    免責聲明!

    本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



     
    粵ICP備18138465號   © 2018-2025 CODEPRJ.COM