ContentNegotiatingViewResolver解析器

来源:互联网 发布:csgo mac版 编辑:程序博客网 时间:2024/06/04 19:23

根据我的理解,ContentNegotiatingViewResolver解析器本身它不解析视图,它是分配任务的,根据请求的类型的不同来分配给不同的视图解析器来解析视图,呈现出不同的view。它有三种方式来设置。

方式1  使用扩展名

http://www.test.com/user.xml    呈现xml文件

http://www.test.com/user.json    呈现json格式

http://www.test.com/user       使用默认view呈现,比如jsp等



方式2  使用http request header的Accept

GET /user HTTP/1.1

Accept:application/xml

 

GET /user HTTP/1.1

Accept:application/json

 

方式3  使用参数

http://www.test.com/user?format=xml

http://www.test.com/user?format=json


如果所有的mediaType都没匹配上,就会使用defaultContentType

<property name="defaultContentType" value="text/html" />


这里是是否启用扩展名支持,默认就是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>



上面的内容没有下面的讲的好


springmvc<一>一种资源返回多种形式【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解析器的时候也可以照下面的配置方式,

配置最后的视图格式可以像下面这样配置,配置为

<property name="viewResolvers">

复制代码
<!--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>
复制代码

声明:我这里的环境是spring4.1

参考:

http://blog.csdn.net/z69183787/article/details/41654603

上边有很好的解释。

有不正之处欢迎指正,谢谢!



阅读全文
0 0