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解析器的时候也可以照下面的配置方式,

<!--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

参考:https://www.cnblogs.com/teach/p/5903718.html 

发布了49 篇原创文章 · 获赞 19 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/shenju2011/article/details/97929309