spring ContentNegotiationManagerFactoryBean 内容协商
来源:互联网 发布:中标数据科技有限公司 编辑:程序博客网 时间:2024/05/18 02:18
一.什么是内容协商
简单点说,就是同一资源,可以有多种表现形式,比如xml、json等,具体使用哪种表现形式,是可以协商的。
这是RESTfull的一个重要特性,Spring Web MVC也支持这个功能。
1.Spring MVC REST是如何决定采用何种方式(视图)来展示内容呢?
一:根据Http请求的header中的Accept属性的值来判读,比如:
Accept: application/xml 将返回xml格式数据
Accept: application/json 将返回json格式数据
优点:是这种方式是理想的标准方式
缺点:是由于浏览器的差异,导致发送的Accept Header头可能会不一样,从而导致服务器不知要返回什么格式的数据
二:根据扩展名来判断,比如:
/mvc/test.xml 将返回xml格式数据
/mvc/test.json 将返回json格式数据
/mvc/test.html 将返回html格式数据
缺点:丧失了同一URL的多种展现方式。在实际环境中使用还是较多的,因为这种方式更符合程序员的习惯
三:根据参数来判断
/mvc/test?format=xml 将返回xml数据
/mvc/test?format=json 将返回json数据
缺点:需要额外的传递format参数,URL变得冗余繁琐,缺少了REST的简洁风范
2.使用内容协商的功能,如果不使用第三种方式的话,3.2的版本可以什么都不用配置,默认就能支持前面两种。下面还是看看怎么配置,示例如下:
需要在spring的配置文件中做配置,示例如下:
<!--1、检查扩展名(如my.pdf);2、检查Parameter(如my?format=pdf);3、检查Accept Header--> <bean id= "contentNegotiationManager" class= "org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <!-- 扩展名至mimeType的映射,即 /user.json => application/json --> <property name= "favorPathExtension" value= "true" /> <!-- 用于开启 /userinfo/123?format=json 的支持 --> <property name= "favorParameter" value= "true" /> <property name= "parameterName" value= "format"/> <!-- 是否忽略Accept Header --> <property name= "ignoreAcceptHeader" value= "false"/><!--扩展名到MIME的映射;favorPathExtension, favorParameter是true时起作用 --> <property name= "mediaTypes"> <value> json=application/json xml=application/xml html=text/html </value> </property><!--<property name="mediaTypes">--> <!--<map>--> <!--<entry key="xml" value="application/xml"/>--> <!--<entry key="json" value="text/plain"/>--> <!--<entry key="xls" value="application/vnd.ms-excel"/>--> <!--</map>--><!--</property>--> <!-- 默认的content type ,在没有扩展名和参数时即: "/user/1" 时的默认展现形式 --> <property name= "defaultContentType" value= "text/html" /> </bean>
视图定义:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="0"/> <property name="contentNegotiationManager" ref="contentNegotiationManager"/> <property name="viewResolvers"> <list> <!-- 这个类用于jsp视图解析 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page/"/> <property name="suffix" value=".jsp"/> </bean> </list> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"> </bean> <!-- for application/xml --> <bean class="org.springframework.web.servlet.view.xml.MarshallingView"> <property name="marshaller"> <bean class="org.springframework.oxm.castor.CastorMarshaller"> <property name="validating" value="false"></property> </bean> </property> </bean> </list> </property> </bean>
在mvc:annotation-driven里面配置使用内容协商<mvc:annotation-driven conversion-service= "conversionService" content-negotiation-manager= "contentNegotiationManager”/>
...
阅读全文
0 0
- spring ContentNegotiationManagerFactoryBean 内容协商
- spring ContentNegotiationManagerFactoryBean 内容协商
- spring ContentNegotiationManagerFactoryBean 内容协商 2017-07-31 14:25 68人阅读 评论(0) 收藏 举报 一.什么是内容协商 简单点说,
- Spring MVC REST内容协商
- 使用Spring MVC的内容协商
- 内容协商
- [金步国]Apache内容协商
- ajax中的内容协商
- 17.1 内容协商技术
- spring REST中的内容协商(同一资源,多种展现:xml,json,html)
- spring REST中的内容协商(同一资源,多种展现:xml,json,html)
- Spring 3.x MVC 入门3 -- 使用内容协商来实现多视图
- ContentNegotiatingViewResolver spring REST中的内容协商(同一资源,多种展现:xml,json,html)
- ContentNegotiatingViewResolver spring REST中的内容协商(同一资源,多种展现:xml,json,html)
- Spring Web MVC框架(九) XML和JSON视图与内容协商
- Spring MVC ContentNegotiation内容协商机制(一个请求路径返回多种数据格式)源码解析
- apache内容协商的配置
- 内容协商 (Content Negotiation)
- storyboard Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]
- flume高并发优化——(16)解决offsets变小问题
- webservice之cxf服务端及客户端实现(maven创建web项目)
- Linux的JCPU与PCPU区别
- #5. 【NOI2014】动物园
- spring ContentNegotiationManagerFactoryBean 内容协商
- python 判断当前句子语种
- ROS实战_1.6 通过ROS同时运行多台机器人
- 四种方法解析xml
- 7月28日云栖精选夜读:【上报纸啦】95后大学生用机器学习PAI大战老年痴呆
- 如何在前端项目中引用bootstrap less?
- -webkit-min-device-pixel-ratio: 2是什么意思?
- 延迟加载的个人理解
- bootstrap作为mixin库的应用模式