天天看点

前端请求为content-type:application/json;后台如何接收

①可以用@RequestBody,接收json数据

@RequestBody(required = false)String str
           

②如果是比较复杂的json数据,可以使用JSON处理

先引入jar包:

<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.3</version>
</dependency>
           

controller代码中写

public CommonResult updateDictionary(@RequestBody(required = false) String obj) {
        //解析json数据
        JSONObject json = JSON.parseObject(obj);
        String description=json.getString("description");
        Object dictionaryItemList1=json.get("dictionaryItemList");
        ...
}