美文网首页
Spring14-JSON

Spring14-JSON

作者: 我相信你爱过gg | 来源:发表于2017-05-07 18:12 被阅读27次

为什么要使用json数据进行交互

json数据格式在接口调用中 html页面中比较常用,因为json格式比较简单,解析比较方便.

在Spring mvc 中客户端请求时,携带数据有两种方式
第一中key/value
第二种json如下图片


1.请求json返回json,要求请求的是json数据,所以在前端页面中需要将请求的内容转成json,不太方便.

2.请求 键值对,输出json.比较常用.

准备环境

Spring Mvc通过 jackson包进行json转换.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.0.pr3</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

JSON 测试

@Controller
@RequestMapping(value = "/test")
public class TestController{

    @RequestMapping(value = "/testMethod", method = {RequestMethod.POST})
    public @ResponseBody Test testMethod(@RequestBody Test test) {
        return test;
    }
}

这个代码就是将我们的请求数据,又给原封不动的响应回去.但是请求数据格式是json,而响应数据也是json格式的.

相关文章

  • Spring14-JSON

    为什么要使用json数据进行交互 json数据格式在接口调用中 html页面中比较常用,因为json格式比较简单,...

网友评论

      本文标题:Spring14-JSON

      本文链接:https://www.haomeiwen.com/subject/dzwutxtx.html