面向API接口开发的时候,经常遇到对接接口数据,而数据一般是json格式的,在这里记录一下使用SpringBoot接收json格式数据的方式
将json数据用字符串去接收,然后转成fastjson的对象(com.alibaba.fastjson.JSONObject)
package boot.example.json.controller;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* 蚂蚁舞
*/
@RestController
@RequestMapping(value="/boot")
public class BootJsonStrController1 {
@PostMapping(value="/demo1")
public Object jsonStr1(@RequestBody String str) {
// 使用fastjson JSONObject
JSONObject jsonData = JSONObject.parseObject(str);
System.out.println(jsonData.toJSONString());
Map<String, Object> map = new HashMap<>();
map.put("state", true);
map.put("code", 200);
map.put("timeStamp", System.currentTimeMillis()/1000);
return map;
}
}
也可以用com.alibaba.fastjson2.JSONObject
package boot.example.json.controller;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* 蚂蚁舞
*/
@RestController
@RequestMapping(value="/boot")
public class BootJsonStrController2 {
@PostMapping(value="/demo2")
public Object jsonStr2(@RequestBody String str) {
// 使用fastjson2 JSONObject
JSONObject jsonData = JSONObject.parseObject(str);
System.out.println(jsonData.toJSONString());
Map<String, Object> map = new HashMap<>();
map.put("state", true);
map.put("code", 200);
map.put("timeStamp", System.currentTimeMillis()/1000);
return map;
}
}
fastjson的maven包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.25</version>
<scope>compile</scope>
</dependency>
还可以使用(com.google.gson.JsonObject)
maven包
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
package boot.example.json.controller;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* 蚂蚁舞
*/
@RestController
@RequestMapping(value="/boot")
public class BootJsonStrController3 {
@PostMapping(value="/demo3")
public Object jsonStr3(@RequestBody String str) {
Gson gson = new Gson();
JsonObject json = gson.fromJson(str, JsonObject.class);
System.out.println(json.toString());
Map<String, Object> map = new HashMap<>();
map.put("state", true);
map.put("code", 200);
map.put("timeStamp", System.currentTimeMillis()/1000);
return map;
}
}
直接使用fastjson的JSONObject对象
package boot.example.json.controller;
import com.alibaba.fastjson.JSONObject;
//import com.alibaba.fastjson2.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* 蚂蚁舞
*/
@RestController
@RequestMapping(value="/boot")
public class BootJsonStrController4 {
@PostMapping(value="/demo4")
public Object jsonStr4(@RequestBody JSONObject jsonObject) {
System.out.println(jsonObject.toString());
Map<String, Object> map = new HashMap<>();
map.put("state", true);
map.put("code", 200);
map.put("timeStamp", System.currentTimeMillis()/1000);
return map;
}
}
能不能使用com.google.gson.JsonObject对象去接收?不能直接用!!!(有其他方式可用,就不去研究这种情况了)
import com.google.gson.JsonObject
// 直接用是不行的
@PostMapping(value="/demoxxx")
public void jsonStr5(@RequestBody JsonObject json) {
System.out.println(json.toString());
}
简单的json数据还可以用java具体的对象的方式去接收,这种方式对于较复杂的json数据处理起来挺麻烦的
@PostMapping(value="/demoxxx")
public void jsonStr6(@RequestBody Object object) {
System.out.println(object.toString());
}