企业网站样式,专门做图片的网站吗,网站栏目页描述怎么写,深圳网站设计公司怎么样一.什么是内容协商
简单点说#xff0c;就是同一资源,可以有多种表现形式#xff0c;比如xml、json等#xff0c;具体使用哪种表现形式#xff0c;是可以协商的。
这是RESTfull的一个重要特性#xff0c;Spring Web MVC也支持这个功能。 1.Spring MVC REST是如何决定采用…一.什么是内容协商
简单点说就是同一资源,可以有多种表现形式比如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?formatxml 将返回xml数据
/mvc/test?formatjson 将返回json数据 缺点需要额外的传递format参数URL变得冗余繁琐缺少了REST的简洁风范 2.使用内容协商的功能如果不使用第三种方式的话3.2的版本可以什么都不用配置默认就能支持前面两种。下面还是看看怎么配置示例如下 需要在spring的配置文件中做配置示例如下
!--1、检查扩展名如my.pdf2、检查Parameter如my?formatpdf3、检查Accept Header-- bean id contentNegotiationManager class org.springframework.web.accept.ContentNegotiationManagerFactoryBean !-- 扩展名至mimeType的映射,即 /user.json application/json -- property name favorPathExtension value true / !-- 用于开启 /userinfo/123?formatjson 的支持 -- 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 jsonapplication/json xmlapplication/xml htmltext/html /value /property !--property namemediaTypes-- !--map-- !--entry keyxml valueapplication/xml/-- !--entry keyjson valuetext/plain/-- !--entry keyxls valueapplication/vnd.ms-excel/-- !--/map-- !--/property-- !-- 默认的content type ,在没有扩展名和参数时即: /user/1 时的默认展现形式 -- property name defaultContentType value text/html / /bean 视图定义
bean classorg.springframework.web.servlet.view.ContentNegotiatingViewResolver property nameorder value0/ property namecontentNegotiationManager refcontentNegotiationManager/ property nameviewResolvers list !-- 这个类用于jsp视图解析 -- bean classorg.springframework.web.servlet.view.InternalResourceViewResolver property nameprefix value/WEB-INF/page// property namesuffix value.jsp/ /bean /list /property property namedefaultViews list bean classorg.springframework.web.servlet.view.json.MappingJackson2JsonView /bean !-- for application/xml -- bean classorg.springframework.web.servlet.view.xml.MarshallingView property namemarshaller bean classorg.springframework.oxm.castor.CastorMarshaller property namevalidating valuefalse/property /bean /property /bean /list /property /bean 在mvc:annotation-driven里面配置使用内容协商
mvc:annotation-driven conversion-service conversionService content-negotiation-manager contentNegotiationManager”/