Spring Boot笔记-controller接收json参数
以前使用的是这样的方式获取JSON参数的
在controller端参数使用HttpServletRequest
HttpServletRequest request
通过他的
request.getInputStream();
//获取输入流后使用
BufferedReader去直接读就可以了
最近发现了一种新的方式,使用@RequestBody注解,即可如下:
@PostMapping("/postJson1")
public Object postJson1(@RequestBody String req){
System.out.println(req);
Map<String, Object> ret = new HashMap<String, Object>();
ret.put("code", 200);
ret.put("msg", "成功");
return ret;
}
@PostMapping("/postJson2")
public Object postJson2(@RequestBody TestObject testObject){
System.out.println(testObject);
Map<String, Object> ret = new HashMap<String, Object>();
ret.put("code", 200);
ret.put("msg", "成功");
return ret;
}
其中TestObject内容如下:
@Data
public class TestObject {
private Integer key1;
private String key2;
}
PostMan设置如下:
内容如下:
还没有评论,来说两句吧...