原文链接:http://www.jianshu.com/p/7097fea8ce3f
@ResponseBody用法
作用:
该注解用于将Controller的方法返回的对象,根据HTTP Request Header的Accept的内容,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
使用时机:
返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用.
配置返回JSON和XML数据
1.添加jackson依赖

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.5.4</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.5.4</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>2.5.4</version>
</dependency>

2.开启<mvc:annotation-driven />

3.代码

@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    @ResponseBody
    public User queryBasic() {
        User user = new User();
        user.setId(10);
        user.setUserName("zhangsan");
        user.setPassword("123456");
        user.setSex(10);
        user.setCreated(new Date());
        return user;
    }
}

User.java

public class User {

    private Integer id;

    // 用户名
    private String userName;

    // 密码
    private String password;

    // 性别,1男性,2女性
    private Integer sex;

    // 创建时间
    private Date created;

    public Integer getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", sex=" + sex +
                ", created=" + created +
                '}';
    }
}

测试

---------------------------------------------------------

总结:

可以看到,想要返回json数据只要配置三个地方就可以了

1.jackson依赖
2.SpringMVC配置文件中加上<mvc:annotation-driven/>注解
3.Controller中的@ResponseBody注解

除此之外并不需要配置转换器什么的,当然这只是使用,其中的原理还是要去了解的

SpringMVC @ResponseBody的使用的更多相关文章

  1. SpringMVC @ResponseBody 415错误处理

    在查看下面部分内容之前,请先检查你的请求蚕食是否正确,如果全部正确,请继续往下看 刚开始用SpringMVC, 页面要使用jQuery的ajax请求Controller. 但总是失败,主要表现为以下两 ...

  2. 解决spring-mvc @responseBody注解返回json 乱码问题

    在使用spring-mvc的mvc的时候既享受它带来的便捷,又头痛它的一些问题,比如经典的中文乱码问题.现在是用json作为客户端和服务端 的数据交换格式貌似很流行,但是在springmvc中有时候会 ...

  3. SpringMvc @ResponseBody字符串中文乱码原因及解决方案

    今天突然发现一个问题,后来在网上也找到了很多解决思路,自己也查找到了问题所在,记录一下. @RequestMapping(value = "/demo1") @ResponseBo ...

  4. SpringMvc @ResponseBody

    一.@Response使用条件 二. @Response在最小配置.jackson的jar包情况下,json中包含的日期类型字段都是以时间戳long类型返回 三. Jack序列化对象转为JSON的限制 ...

  5. SpringMVC @ResponseBody返回中文乱码

    SpringMVC的@ResponseBody返回中文乱码的原因是SpringMVC默认处理的字符集是ISO-8859-1, 在Spring的org.springframework.http.conv ...

  6. springmvc @responsebody 406/415问题解决

    提供几个解决思路 1.如果项目中用的spring jar包是4.x版本, 需要jackson-annotations-2.x/jackson-core-2.x/jackson-databind-2.x ...

  7. SpringMVC ResponseBody返回中文乱码解决方案

    @RequestMapping(value = "/getForm") @ResponseBody public List<String> getForm(String ...

  8. SpringMVC @ResponseBody 406

    使用@ResponseBody注解可以让Controller返回json格式的数据,在需要传输一个对象信息的时候往往使用这种方式.如果在使用的时候遇到了406,一般原因是: 缺少jar包.转换成jso ...

  9. SpringMVC @ResponseBody和@RequestBody使用

    @ResponseBody用法 作用: 该注解用于将Controller的方法返回的对象,根据HTTP Request Header的Accept的内容,通过适当的HttpMessageConvert ...

随机推荐

  1. POJ 3280 Cheapest Palindrome(DP)

    题目链接 被以前的题目惯性思维了,此题dp[i][j],代表i到j这一段变成回文的最小花费.我觉得挺难的理解的. #include <cstdio> #include <cstrin ...

  2. JavaScript验证函数大全

    1. 长度限制 <script> function test() { if(document.a.b.value.length>50) { alert("不能超过50个字符 ...

  3. [转]Efficiently Paging Through Large Amounts of Data

    本文转自:http://msdn.microsoft.com/en-us/library/bb445504.aspx Scott Mitchell April 2007 Summary: This i ...

  4. JS对json对象的调用成员2种方式

    1,控制台上对json对象的调用 //控制台上对json对象的调用 var a={"a":"1","b":"2"} a. ...

  5. .NET开发问题汇总

    问题一: mvc 部署到iis 提示错误未能加载文件或程序集System.Web.Http.WebHost 解决方案: Nuget程序包管理 —>程序包管理控制台,运行以下命令即可:Update ...

  6. Maven问题总结:could not resolve archetype xxxxxxx from any of the configured repositories

    错误提示 Eclipse中通过Archetype创建Maven项目时报错:Could not resolve archetype xxxxxxx from any of the configured ...

  7. [听听音乐]when you believe [singer: mariah carey]

    movie: prince of egypt Lyrics   Many nights we prayed With no proof anyone could hear In our hearts ...

  8. Open vSwitch使用案例扩展实验

    参考:Open vSwitch使用案例扩展实验 实验目的: 通过python脚本调用OpenvSwitch命令: 学习Mininet基于python脚本创建拓扑的实现: 进一步深度使用"ov ...

  9. 动态样式语言Less学习笔记

    介绍资料参见:http://www.bootcss.com/p/lesscss/ LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承,运算, 函数. LESS 既可以在 客户端 上运行 (支 ...

  10. what is php?

    PHP is a popular general-purpose scripting language that is especially suited to web development. Fa ...