org.springframework.web.servlet.view.ContentNegotiatingViewResolver
restful服务中一个重要的特性就是一种资源可以有多种表现形式,在springmvc中可以使用ContentNegotiatingViewResolver这个视图解析器来实现这种方式。
描述资源的三种形式
一、使用扩展名
http://localhost:8080/test/user.xml 以xml格式呈现
http://localhost:8080/test/user.json 以json格式呈现
http://localhost:8080/test/user 以默认视图呈现,如jsp
二、使用http request header的Accept
GET /user HTTP/1.1
Accept:application/xml 请求时设置返回形式是xml,如使用ajax请求,则需要设置contentType:application/xml
GET /user HTTP/1.1
Accept:application/json 请求时设置返回形式是json,如使用ajax请求,则需要设置contentType:application/json
三、使用参数
http://localhost:8080/test/user?format=json
http://localhost:8080/test/user?format=xml
上面了解了同一种资源的三种呈现方式,即json、xml、jsp,那么我们要如何使用ContentNegotiatingViewResolver类配置,使客户端请求的方式不同,返回同一种资源的三种方式呢?
ContentNegotiatingViewResolver配置
ContentNegotiatingViewResolver是视图解析器,我们在使用jsp这个视图的时候也配置了一个视图解析器InternalResourceViewResolver,他们都是视图解析器,后者着重在配置一个默认的视图解析即jsp;ContentNegotiatingViewResolver本身不会解析,他会分配其他的viewResolver去解析,并选择一个看起来像是客户端请求需要返回的一种 View 返回。
下面是ContentNegotiatingViewResolver的具体配置

<!--springmvc中根据后缀不同返回不同格式的配置
如,XXX.json 返回json格式
XXX.xml 返回xml格式
xxx 返回jsp
-->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<!--这里是解析器的执行顺序,如果有多个的话,配置的数值越小,则越早执行-->
<property name="order" value="1" />
<!--
这里是是否启用扩展名支持,默认就是true
例如 /user/{userid}.json
-->
<property name="favorPathExtension" value="true"></property> <!--这里是是否启用参数支持,默认就是true
例如 /user/{userid}?format=json
-->
<property name="favorParameter" value="false"></property>
<!--这里是否忽略掉accept header,默认就是false
例如 GET /user HTTP/1.1
Accept:application/json
--> <property name="ignoreAcceptHeader" value="true"></property>
<!-- 这里是扩展名到mimeType的映射,
例如 /user/{userid}.json 中的 .json 就会映射到 application/json
-->
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" /> <entry key="xml" value="application/xml" />
</map>
</property>
<!--视图-->
<property name="defaultViews">
<list>
<!--json视图-->
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"></bean>
<!--xml视图-->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView"
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.cn.my.entity.Course</value>
<value>com.cn.my.entity.CourseList</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
</list>
</property>
</bean>

order:如果存在多个viewResolver则order值小的被使用,如果没有合适的viewResolver则会使用另外的;
favorPathExtension:是否支持扩展名,默认为true(支持),扩展名指的xxx.json、xxx.xml等形式
favorParameter:是否启用参数支持,默认为true(支持),即xxx?format=json、xxx?format=xml等形式,这里的参数名默认为format,可以通过配置改变。
ignoreAcceptHeader:是否忽略accept header,默认是false(不忽略),即请求时指定的contentType:application/json等,由于我这里要使用扩展名的形式返回,所以把另外两项都关闭了,可视不同情况,使用不同设置;
mediaTypes:配置扩展名到mimeType的映射,这里配置了json和xml的映射;
defaultViews:配置视图,这里配置了json和xml的视图,json使用的jackson;
最后,我还配置一个另外一个视图解析器,InternalResourceViewResolver,

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>

这是jsp的视图解析器,order属性配置为了2,在无法匹配到json、xml的情况下,会返回jsp的视图。
下面是controller的方法

package com.cn.my.controllor; import java.util.ArrayList;
import java.util.List; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import com.cn.my.entity.Course;
import com.cn.my.entity.CourseList; @Controller
@RequestMapping("/mul")
public class MultiView { @RequestMapping("/simple/{coursId}")
public String method1(@PathVariable("coursId") String coursId,ModelMap model){
Course c=new Course();
c.setId(coursId);
c.setContent("这是测试内容");
c.setName("李四");
model.put("course", c);
return "course";
}
}

这里使用的restful服务中的uri的格式,用到了@PathVariable注解,这里方法返回的String,且没有@ResponseBody注解,在前边的返回json一文中有返回json的配置,需要@ResponseBody注解,详细的可以参看前边,同时在方法参数中有ModelMap,为什么这里要返回一个字符串呢,目的是为了统一,我们知道如果要返回到jsp视图,那么这里要返回的一个代表逻辑视图名的字符串,为了使三种方式统一,这里返回的是字符串,如果不返回到jsp也可以返回一个实际的对象。
下面看测试结果,
请求:http://localhost:8081/springmvc/mul/simple2/1212.json

请求:http://localhost:8081/springmvc/mul/simple2/1212.xml

请求:http://localhost:8081/springmvc/mul/simple2/1212

最后一个jsp的视图,本来是要在jsp页面中输出内容的,我这里没做,只是输出了一段话。请谅解!
从上边的测试结果来看,我们分别使用了三种不同的请求方式去请求同一个资源,返回了各自的形式,这种方式很适合用在不同的系统调用同一个系统时,可能别的系统处理数据的方式不一样,我们使用上边的配置可以实现一套代码,返回不同的形式。
最后
在配置默认的jsp解析器的时候也可以照下面的配置方式,

<!--springmvc中根据后缀不同返回不同格式的配置
如,XXX.json 返回json格式
XXX.xml 返回xml格式
xxx 返回jsp
-->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<!--这里是解析器的执行顺序,如果有多个的话,配置的数值越小,则越早执行-->
<property name="order" value="1" />
<!--
这里是是否启用扩展名支持,默认就是true
例如 /user/{userid}.json
-->
<property name="favorPathExtension" value="true"></property> <!--这里是是否启用参数支持,默认就是true
例如 /user/{userid}?format=json
-->
<property name="favorParameter" value="false"></property>
<!--这里是否忽略掉accept header,默认就是false
例如 GET /user HTTP/1.1
Accept:application/json
--> <property name="ignoreAcceptHeader" value="true"></property>
<!-- 这里是扩展名到mimeType的映射,
例如 /user/{userid}.json 中的 .json 就会映射到 application/json
-->
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" /> <entry key="xml" value="application/xml" />
</map>
</property> <!--视图解析器-->
<property name="viewResolvers">
<list> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
</list> </property>
<!--视图-->
<property name="defaultViews">
<list>
<!--json视图-->
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"></bean>
<!--xml视图-->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView"
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.cn.my.entity.Course</value>
<value>com.cn.my.entity.CourseList</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
</list>
</property>
</bean>
org.springframework.web.servlet.view.ContentNegotiatingViewResolver的更多相关文章
- NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.web.servlet.view.InternalResourceViewResolver' available
问题描述: 项目中需要配置多个视图解析器,所以使用ContentNegotiatingViewResolver来处理,在其内部设置了FreeMarkerViewResolver .InternalRe ...
- org.springframework.web.servlet.view.InternalResourceViewResolver
http://blog.csdn.net/superdog007/article/details/28857495 我们在controller里面经常这样return一个ModelAndView: r ...
- Cannot find class [org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer]
解决方案:添加spring-webmvc好多人都不知道org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer这个类到底 ...
- org.springframework.web.servlet.view
view包下面的类和接口 description:提供view和viewResolver的标准实现,也提供一些抽象基类.Spring MVC已经提供了 JSPs, Velocity, XSLT等视图的 ...
- org.springframework.web.servlet.PageNotFound
2017-07-11 16:36:13.489 WARN [http-nio-8032-exec-16]org.springframework.web.servlet.PageNotFound -Re ...
- org.springframework.web.servlet.DispatcherServlet noHandlerFound
1 请求URL: http://localhost:8080/mvc/rojas 2 control RequestMapping : @RequestMapping(value="xx ...
- 静态资源[org.springframework.web.servlet.PageNotFound]
springmvc 无法访问js.css.jpg等资源文件,tomcat启动报警告如下 [org.springframework.web.servlet.PageNotFound] - No mapp ...
- org.springframework.web.servlet.PageNotFound No mapping found for HTTP request with URI [/AssetRepair/assetRepairController/test.do] in DispatcherServlet with name 'assetrepair'
web.xml文件配置: xxx-servlet.xml 我们可以发现DispatcherServlet会处理"jsp"后缀的请求;而模型视图后缀也是jsp的 如果这样配置会报以下 ...
- springmvc报错 org.springframework.web.servlet.DispatcherServlet
在写springMVC时,导入所有需要的包后,运行程序,控制台报以下错误: 严重: Servlet [springDispatcherServlet] in web application [/Spr ...
随机推荐
- Flume HA
flume提供fail over和load balance功能 1.添加collector配置(配置两个collector) # Name the components on this agents1 ...
- ansible自动化运维管理工具
1.Ansible介绍 1)Ansible:Ansible的核心程序 2)Host Inventory:(默认路径:/etc/ansible/hosts)记录了每一个由Ansible管理的主机信息,信 ...
- Ubuntu16.04安装NVIDIA驱动、实现GPU加速
NVIDIA驱动前前后后装了好几遍,下面把个人的经验分享下,大家仅供参考. 老规矩,先引用师兄的(最详细)https://blog.csdn.net/sinat_23853639/article/de ...
- SQL子连接案例
子查询 何时使用子查询 1. 子查询作为数据源 2. 数据加工 需求:根据不同顾客的所有的账户余额划分区间,进行分组 sql语句实现如下: select 'Small Fry' name , 0 lo ...
- switch-case的选择用法
企业发放的奖金根据利润提成.利润I低于或等于100000元的,奖金可提0.1:利润高于100000元,低于200000(100000<I<=200000)时,低于100000元的部分按10 ...
- 浅析射线检测 raycast 的使用 !Cocos Creator 3D !
哎呀?为什么我设置了节点点击回调没反应呀? 记得在写小鸡拍拍的时候遇到一个问题,想要捕捉排球的点击事件,按照 2d 的写法,给3d 节点添加 node 事件,结果点了没反应.代码大概是以下的样子. t ...
- [转帖]浅谈IOC--说清楚IOC是什么
浅谈IOC--说清楚IOC是什么 Need Study https://www.cnblogs.com/DebugLZQ/archive/2013/06/05/3107957.html 博文目录 1. ...
- php中文网--JavaScript
PHP中文网:http://www.php.cn/course/18.html 常用的两个客户端输出方法 document.write("你好呀js"); 描述:在网页的<b ...
- Django 前端通过json 取出后端数据
Django 前端通过json 取出后端数据 前端通过json 取出后端数据 步骤1:后台数据通过 JSON 序列化成字符串a 注意:1.json是1个字符串 2.通过json.dumps('xx ...
- 自己动手实现一个html2canvas
前言 昨天写了新手引导动画的4种实现方式,里面用到了 html2canvas 于是就顺便了解了一下实现思路. 大概就是 利用 svg 的 foreignObject 标签, 嵌入 dom, 最后再利用 ...