@PathVariable
- 能匹配url模板中的占位符对应参数
- 支持get和post请求
- Controller声明:
//get-uri: localhost:8080/test/pathVariable/123
@GetMapping(value = "pathVariable/{param}")
public String getPathVariable(@PathVariable("param") String id) {
return id;
}
//post-uri: localhost:8080/test/pathVariable/123
@PostMapping(value = "pathVariable/{param}")
public String postPathVariable(@PathVariable("param") String id) {
return id;
}
复制代码
- RestTemplate访问:
public static void pathVariable() {
Map map = new HashMap<>();
map.put("paramName", "name1");
RestTemplate restTemplate = new RestTemplate();
//get调用使用getForEntity
ResponseEntity responseEntity = restTemplate.getForEntity(host+"/test/pathVariable/{paramName}", String.class, map);
//post需要用postForEntity方法调用
//ResponseEntity responseEntity = restTemplate.postForEntity(host+"/test/pathVariable/{paramName}", null, String.class, map);
System.out.println("getPathVariable2: " + responseEntity.getBody());//输出“name1”
}
复制代码
@RequestParam
- 与PathVariable类似,RequestParam只能用于url参数传值,可传入简单类型、指定映射参数名、指定是否必填、设置默认值等
- 支持get和post请求
- Controller声明:
//get-uri: localhost:8080/test/requestParam?param=xxx
@GetMapping(value = "test/requestParam")
public String getRequestParam(@RequestParam(defaultValue = "ssss") String param) {
//当请求url为localhost:8080/test/requestParam时,param为默认值ssss
//post-uri: localhost:8080/test/requestParam?param=xxx
@PostMapping(value = "test/requestParam")
public String getRequestParam(@RequestParam(name = "aa", required = false) String param) {
//指定参数为“aa”,即当url为localhost:8080/test/requestParam?aa=p1时,param为p1
//当url为localhost:8080/test/requestParam时,param为null
//post-uri: localhost:8080/test/requestParam
@PostMapping(value = "test/requestParam")
public String postRequestParam(@RequestParam(required = false) int param) {
// int是基本类型,即使设置required=false,因为int无法设置为null仍然会报错,可以将int替换为Integer
复制代码
- RestTemplate访问:
public static void requestParam() {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity responseEntity = restTemplate.getForEntity(host+"/test/requestParam",
String.class);
System.out.println("getRequestParam: " + responseEntity.getBody());//url未指定,默认值为ssss
Map map = new HashMap<>();
map.put("param", "name3");
responseEntity = restTemplate.getForEntity(host+"/test/requestParam?param={param}", String.class);
System.out.println("getRequestParam: " + responseEntity.getBody());//输出name3
}
public static void requestParam2() {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity responseEntity = restTemplate.postForEntity(host+"/test/requestParam",
String.class);
System.out.println("postRequestParam: " + responseEntity.getBody());//未指定aa,输出结果为null
}
复制代码
@RequestBody
- RequestBody主要通过请求体body传值,常用来处理content-type=application/json请求,同时支持get和post请求,能获取到body中的数据并转化为对象
- 支持get和post请求
- Controller声明:
//get-uri: localhost:8080/test/requestBody
@GetMapping(value = "test/requestBody")
public BaseParam getRequestBody(@RequestBody BaseParam param) {
return param;
}
//post-uri: localhost:8080/test/requestBody
@PostMapping(value = "test/requestBody")
public BaseParam postRequestBody(@RequestBody BaseParam param) {
return param;
}
复制代码
- RestTemplate访问:
//get请求
//RestTemplate的get请求不支持设置body,所以使用restTemplate无法调用get请求的@RequestBody注解,但使用PostMan等工具是可以发送带body的get请求的
//post请求
public static void postRequestBody() {
HttpHeaders requestHeaders = new HttpHeaders();
//注意设置content-type
requestHeaders.add("Content-type", MediaType.APPLICATION_JSON_VALUE);
BaseParam param = new BaseParam();
param.setParamName("name4");
HttpEntity requestEntity = new HttpEntity<>(param, requestHeaders);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity responseEntity = restTemplate.postForEntity(host+"/test/requestBody",
requestEntity, BaseParam.class);
System.out.println("postRequestBody: " + responseEntity.getBody().getParamName());//输出name4
}
复制代码
@ModelAttribute
- ModelAttribute注解参数时,发送请求的content-type需要为"x-www-form-urlencoded"
- 仅支持post请求且content-type=x-www-form-urlencoded能接收到数据;如果是post请求且content-type=application/json、get请求且content-type=x-www-form-urlencoded、get请求且content-type=application/json均不会报错但无法接收到数据
- Controller声明:
@PostMapping(value = "test/modelAttribute")
public String postModelAttribute(@ModelAttribute BaseParam param) {
return param.getParamName();
}
复制代码
- RestTemplate访问:
public static void postModelAttribute() {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Content-type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
MultiValueMap params= new LinkedMultiValueMap();
params.add("account", "用户名");
params.add("paramName", "123456");
HttpEntity> requestEntity = new HttpEntity<>(params, requestHeaders);
RestTemplate restTemplate = new RestTemplate();
//需要提供自定义convert
restTemplate.getMessageConverters().add(new TestMappingJackson2HttpMessageConverter());
ResponseEntity responseEntity = restTemplate.postForEntity(host+"/test/modelAttribute",
requestEntity, String.class);
System.out.println("postModelAttribute: " + responseEntity.getBody());//输出123456
}
//自定义convert
public class TestMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
public TestMappingJackson2HttpMessageConverter() {
List mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.TEXT_PLAIN);
//添加form-urlencoded类型转换
mediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
setSupportedMediaTypes(mediaTypes);
}
}
复制代码
以上对@PathVariable、@RequestParam、@RequestBody、@ModelAttribute的使用方式及RestTemplate如何调用相应注解接口做了总结,如有不对欢迎指出,谢谢!