关于SpringBoot接收json格式的Demo案例
作者:蚂蚁舞
这篇文章主要介绍了关于SpringBoot接收json格式的Demo案例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
SpringBoot接收json格式的Demo
面向API接口开发的时候,经常遇到对接接口数据,而数据一般是json格式的
在这里记录一下使用SpringBoot接收json格式数据的方式
使用SpringBoot的@RequestBody注解
将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());
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
