ssslinppp

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

概要


要实现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  (不是上述提到的后缀名)

 

其他


博客:
  淘宝-代做毕设:






附件列表

     

    posted on 2015-07-25 16:23  ssslinppp  阅读(3635)  评论(0编辑  收藏  举报