SSM实战之返回json

1.返回普通的json

返回字符串,实体类

三个步骤
  • Jackson jar包是否存在于工程

    1
    2
    3
    4
    // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.8'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.8'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.8.8'
  • <mvc:annotation-driven/> 是否配置(默认配置方案,不支持返回map)

  • 返回方法上面是否添加了@ResponseBody注解。

    1
    2
    3
    4
    5
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    @ResponseBody
    public String test( User user,HttpServletRequest req, HttpServletResponse response){
    return "json数据";
    }

2.map自动转换json ,<mvc:annotation-driven/> 的自定义配置

解决springmvc ResponseBody请求返回406的问题

注意不能用.html后缀