spring boot后台接收json方式整理
第一类:请求路径参数
1、@PathVariable
获取路径参数。即url/{id}这种形式。
2、@RequestParam
获取查询参数。即url?name=这种形式
例子
GET
http://localhost:8080/demo/123?name=suki_rong
对应的java代码:
@GetMapping("/demo/{id}")
public void demo(@PathVariable(name = "id") String id, @RequestParam(name = "name") String name) {
System.out.println("id="+id);
System.out.println("name="+name);
}
1
2
3
4
5
输出结果:
id=123
name=suki_rong
第二类:Body参数
因为是POST请求,这里用Postman的截图结合代码说明
1、@RequestBody
例子
对应的java代码:
@PostMapping(path = "/demo1")
public void demo1(@RequestBody Person person) {
System.out.println(person.toString());
}
1
2
3
4
输出结果:
name:suki_rong;age=18;hobby:programing
也可以是这样
@PostMapping(path = "/demo1")
public void demo1(@RequestBody Map<String, String> person) {
System.out.println(person.get("name"));
}
1
2
3
4
输出结果:
suki_rong
2、无注解
例子
对应的java代码:
@PostMapping(path = "/demo2")
public void demo2(Person person) {
System.out.println(person.toString());
}
1
2
3
4
输出结果:
name:suki_rong;age=18;hobby:programing
Person类
public class Person {
private long id;
private String name;
private int age;
private String hobby;
@Override
public String toString(){
return "name:"+name+";age="+age+";hobby:"+hobby;
}
// getters and setters
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
第三类:请求头参数以及Cookie
1、@RequestHeader
2、@CookieValue
例子
java代码:
@GetMapping("/demo3")
public void demo3(@RequestHeader(name = "myHeader") String myHeader,
@CookieValue(name = "myCookie") String myCookie) {
System.out.println("myHeader=" + myHeader);
System.out.println("myCookie=" + myCookie);
}
1
2
3
4
5
6
也可以这样
@GetMapping("/demo3")
public void demo3(HttpServletRequest request) {
System.out.println(request.getHeader("myHeader"));
for (Cookie cookie : request.getCookies()) {
if ("myCookie".equals(cookie.getName())) {
System.out.println(cookie.getValue());
}
}
}
二、spring接收json字符串的两种方式
1、通过@RequestBody
接收json
直接通过@RequestBody
的方式,直接将json的数据注入到了JSONObject或者用Map接收或者写一个入参的实体类bean对象接收里。
@RestController @RequestMapping("/firstCon") public class FirstCon { @RequestMapping(value = "/abc/get", method = RequestMethod.POST) public String get(@RequestBody Map o) { /*@RequestBody JSONObject o @RequestParameter("name") String name,@RequestParameter("sex") String sex //非json字符串接收方式 eg:get方式;post:form-data or application/x-www-form-urlencoded @RequestBody UserIn user //定义一个实体类接收*/ String name = (String) o.get("name"); String sex = (String) o.get("sex"); return name + ";" + sex; } }
2、通过Request获取
通过request的对象来获取到输入流,然后将输入流的数据写入到字符串里面,最后转化为JSON对象。
@ResponseBody @RequestMapping(value = "/request/data", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") public String getByRequest(HttpServletRequest request) { //JSONObject JSONObject jsonParam = this.getJSONParam(request); return jsonParam.toJSONString(); } public JSONObject getJSONParam(HttpServletRequest request){ JSONObject jsonParam = null; try { // 获取输入流 BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8")); // 数据写入Stringbuilder StringBuilder sb = new StringBuilder(); String line = null; while ((line = streamReader.readLine()) != null) { sb.append(line); } jsonParam = JSONObject.parseObject(sb.toString()); System.out.println(jsonParam.toJSONString()); } catch (Exception e) { e.printStackTrace(); } return jsonParam; }
当然你也可以在前端传入的是json字符串,后台按字符串string参数接收再解析。此时contentType不能设置为application/json。
后来自己又查阅了一些资料,整理一下:
$.ajax 的参数contentType 和 dataType
- contentType 主要设置你发送给服务器的格式
- dataType设置你收到服务器数据的格式。
在 jquery 的 ajax 中, contentType都是默认的值:application/x-www-form-urlencoded,这种格式的特点就是,name/value 成为一组,每组之间用 & 联接,而 name与value 则是使用 = 连接。如: wwwh.baidu.com/q?key=fdsa&lang=zh 这是get , 而 post 请求则是使用请求体,参数不在 url 中,在请求体中的参数表现形式也是: key=fdsa&lang=zh的形式。
注解@ResponseBody 、produces = "application/json;charset=UTF-8" 都是设置返回格式是json
https://www.hangge.com/blog/cache/detail_2485.html
https://www.hangge.com/blog/cache/detail_2483.html