使用spring boot2X做后端,postman做前端测试
1.获取json字符串
@RestController public class Demo { @RequestMapping("test") public Result test(@RequestBody JSONObject obj) { return Result.success(200,obj); } }
测试
2.获取出传入的参数值
@RestController public class Demo { @RequestMapping("test") public Result test(@RequestParam(value = "name",required = false) String name,@RequestParam(value = "id") Integer id) { MyData my = new MyData(); my.setId(id); my.setName(name); return Result.success(200,my); } }
测试
3.获取路径中的参数值
(1)通过PathVariable注解来绑定请求路径的参数
@RestController public class Demo { @RequestMapping(value = "/test/{id}/{name}") public Result test(@PathVariable(value = "id") Integer id, @PathVariable(value = "name") String name) { MyData my = new MyData(); my.setId(id); my.setName(name); return Result.success(200,my); } }
测试
(2)指定前端url请求参数名称与方法名一致
@RestController public class Demo { @RequestMapping(value = "test") public Result test(Integer id,String name) { MyData my = new MyData(); my.setId(id); my.setName(name); return Result.success(200,my); } }
(3)通过HttpServletRequest来获取前端页面参数
@RestController public class Demo { @RequestMapping(value = "test") public Result test(HttpServletRequest request) { MyData my = new MyData(); my.setId(Integer.valueOf(request.getParameter("id"))); my.setName(request.getParameter("name")); return Result.success(200,my); } }
(4)通过RequestParam注解来获取
@RestController public class Demo { @RequestMapping(value = "test") public Result test(@RequestParam(defaultValue = "0") Integer id,@RequestParam(defaultValue = "0")String name) { MyData my = new MyData(); my.setId(id); my.setName(name); return Result.success(200,my); } }
4.获取请求的报文头
@RestController public class Demo { @RequestMapping(value = "test") public Result test(@RequestHeader Map<String, String> map) { return Result.success(200,map); } }
5.请求中的cookie
@RestController public class Demo { @RequestMapping(value = "test") public Result test(@CookieValue(value = "JSESSIONID", required = true) String jsessionId,@CookieValue(value = "name", required = true) String name) { return Result.success(200,"JSESSIONID="+jsessionId+",name="+name); } }
5.获取矩阵变量绑定的参数
矩阵变量
矩阵变量可以出现在任何路径片段中,每一个矩阵变量都用分号(;)隔开,多个值可以用逗号隔开
Spring4.0已经全面支持Matrix Variable,该注解似的开发人员能够将请求中的矩阵变量绑定到处理器的方法参数中
spring boot 默认是无法使用矩阵变量绑定参数的。需要覆盖WebMvcConfigurer中的configurePathMatch方法
package com.example.demo.core; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.*; import org.springframework.web.util.UrlPathHelper; @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper=new UrlPathHelper(); urlPathHelper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(urlPathHelper); } }
使用矩阵变量
@RestController public class Demo { @RequestMapping(value="test/{param}") public Object test(@MatrixVariable(pathVar="param",value="color")String[] tt){ for (String s : tt) { System.out.println(s); } return Result.success(200,tt); } }
调用时也可以写成http://127.0.0.1:8080/test/color=red;color=green;color=blue,结果是一样的
@RestController public class Demo { @RequestMapping(value="test/{x1}/p/{x2}") public Object test2(@MatrixVariable(pathVar = "x1") Map<String, String[]> x1, @MatrixVariable(pathVar = "x2") Map<String, String[]> x2){ System.out.println(x1); System.out.println(x2); return x1+"-----"+x2; } }
附:
Result.java
package com.example.demo.core; import com.alibaba.fastjson.JSON; /** * Created by Beibei on 19/02/22 * API响应结果 */ public class Result<T> { private int code; private String message; private T data; public Result setCode(Integer code) { this.code = code; return this; } public int getCode() { return code; } public String getMessage() { return message; } public Result setMessage(String message) { this.message = message; return this; } public T getData() { return data; } public Result setData(T data) { this.data = data; return this; } @Override public String toString() { return JSON.toJSONString(this); } public static <T> Result<T> fail(Integer code,T data) { Result<T> ret = new Result<T>(); ret.setCode(code); ret.setData(data); return ret; } public static <T> Result<T> failMessage(Integer code,String msg) { Result<T> ret = new Result<T>(); ret.setCode(code); ret.setMessage(msg); return ret; } public static <T> Result<T> successMessage(Integer code,String msg) { Result<T> ret = new Result<T>(); ret.setCode(code); ret.setMessage(msg); return ret; } public static <T> Result<T> success(Integer code,T data) { Result<T> ret = new Result<T>(); ret.setCode(code); ret.setData(data); return ret; } }
MyData.java
package com.example.demo.domain; public class MyData { private int id; private String name; private String other; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getOther() { return other; } public void setOther(String other) { this.other = other; } }