SpringMVC返回XML或者JSON格式的数据

SpringMVC的web.xml配置我就不说了…

首先需要加入json的jar包: 
jackson-mapper-asl-*.jar 
jackson-core-asl-*.jar

在applicationContext-servlet.xml文件配置中加入如下注册默认的消息转换器:

<mvc:annotation-driven/>
  • 1
  • 1

controller:

    @RequestMapping(method=RequestMethod.GET,value="/rest")
    @ResponseBody
    public User rest测试(HttpServletRequest request, UserVo vo){
        User user = new User();
        user.setName("yp");
        user.setPassword("TC");
        return user;
    } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

User类:

@XmlRootElement
public class User implements Serializable{

    private Integer id;

    private String name;

    private String password;

    @XmlElement
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

注意上面的User类必须用@XmlRootElement标注,不然会当Json返回的..

然后直接在浏览器中输入url地址访问就行了。

上面在Spring配置文件中加了< mvc:annotation-driven/>debug你会发现如下图: 
这里写图片描述

里面默认注册了XMl的转换器还有JSON的转换器…

当你在User类上面加@XmlRootElement就会用XML转换器返回xml格式的数据,如果没有用@XmlRootElement那么返回json格式的数据。

当然,转换成什么格式的数据是由请求头的accept属性来指定的。 
如下请求会返回xml格式的数据:

扫描二维码关注公众号,回复: 1465679 查看本文章
   function jsonTest(){
        $.get("rest.htm","",function(res) {
                alert(res);
            //console.log(res);
            },"xml");
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

如下请求会返回json格式的数据:

   function jsonTest(){
        $.get("rest.htm","",function(res) {
                alert(res);
            //console.log(res);
            },"json");
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

如果想看具体的SpringMVC数据转换流程这里给个链接:http://www.chawenti.com/articles/23596.html

当然还有其他的配置方式,比如不使用默认注册的转换器,可以在< mvc:annotation-driven>里面配置

<mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>application/json</value>
                            <value>text/json</value>
                        </list>
                    </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

或者

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean
                    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>application/json</value>
                            <value>text/json</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

这两种方式差不多…这个我也是从网上看的。

猜你喜欢

转载自blog.csdn.net/linpe/article/details/72799678