自己遇到的问题,网上搜了很多都不是很靠谱。决定自己记录一下。
首先总结下来是 @RequestBody 和 @RequestParam在使用Conten-Type的时候有区别。
@RequestBody 处理HttpEntity传递过来的数据,一般用来处理非Content-Type: application/x-www-form-urlencoded编码格式的数据。 如果要用@RequestBody来接受restTemplate的参数,那么你在设置请求头的时候要用
headers.add(“Content-Type”,“application/json”);并且添加参数的集合可以是实体类,也可以是Hashmap.
@RequestMapping("test2")
public Object test2(){
RestTemplate restTemplate =new RestTemplate();
Test test = new Test();
test.setId("123");
test.setName("name");
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type","application/json");
HttpEntity formEntity = new HttpEntity(test, headers);
ResponseEntity<String> stringResponseEntity = null;
String url = "http://localhost:8090/test/test1";
try {
stringResponseEntity = restTemplate.postForEntity(url, formEntity, String.class);
} catch (RestClientException e) {
e.printStackTrace();
}
int statusCodeValue = stringResponseEntity.getStatusCodeValue();
String body = stringResponseEntity.getBody();
Test wrapper = JSONObject.parseObject(body, Test.class);
return wrapper;
}
@RequestParam 用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容。(Http协议中,如果不指定Content-Type,则默认传递的参数就是application/x-www-form-urlencoded类型)
如果要用@RequestParam来接受参数,那么设置请求头要用 headers.add(“Content-Type”,“application/x-www-form-urlencoded”);并且添加的参数集合要使用MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<String, String>();
@RequestMapping("test3")
public Object test3(){
RestTemplate restTemplate =new RestTemplate();
MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<String, String>();
paramMap.set("id","dddd");
paramMap.set("name","fff");
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type","application/x-www-form-urlencoded");
HttpEntity formEntity = new HttpEntity(paramMap, headers);
ResponseEntity<String> stringResponseEntity = null;
String url = "http://localhost:8090/test/test";
try {
stringResponseEntity = restTemplate.postForEntity(url, formEntity, String.class);
} catch (RestClientException e) {
e.printStackTrace();
}
int statusCodeValue = stringResponseEntity.getStatusCodeValue();
String body = stringResponseEntity.getBody();
Test wrapper = JSONObject.parseObject(body, Test.class);
return wrapper;
}
总的测试代码如下
package com.example.tkmybatistest.controller;
import com.alibaba.fastjson.JSONObject;
import com.example.tkmybatistest.entity.Test;
import com.example.tkmybatistest.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
TestService testService;
@RequestMapping("/test")
public Object test( @RequestParam("id") String id,@RequestParam("name") String name){
Test test = new Test();
test.setName("xuly");
test.setId("123");
return test;
}
@RequestMapping("/test1")
public Object test1(@RequestBody Test test){
Test test1 = new Test();
test1.setName("test");
test1.setId("zhangjun");
return test1;
}
@RequestMapping("test2")
public Object test2(){
RestTemplate restTemplate =new RestTemplate();
Test test = new Test();
test.setId("123");
test.setName("name");
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type","application/json");
HttpEntity formEntity = new HttpEntity(test, headers);
ResponseEntity<String> stringResponseEntity = null;
String url = "http://localhost:8090/test/test1" ;
try {
stringResponseEntity = restTemplate.postForEntity(url, formEntity, String.class);
} catch (RestClientException e) {
e.printStackTrace();
}
int statusCodeValue = stringResponseEntity.getStatusCodeValue();
String body = stringResponseEntity.getBody();
Test wrapper = JSONObject.parseObject(body, Test.class);
return wrapper;
}
@RequestMapping("test3")
public Object test3(){
RestTemplate restTemplate =new RestTemplate();
MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<String, String>();
paramMap.set("id","dddd");
paramMap.set("name","fff");
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type","application/x-www-form-urlencoded");
HttpEntity formEntity = new HttpEntity(paramMap, headers);
ResponseEntity<String> stringResponseEntity = null;
String url = "http://localhost:8090/test/test";
try {
stringResponseEntity = restTemplate.postForEntity(url, formEntity, String.class);
} catch (RestClientException e) {
e.printStackTrace();
}
int statusCodeValue = stringResponseEntity.getStatusCodeValue();
String body = stringResponseEntity.getBody();
Test wrapper = JSONObject.parseObject(body, Test.class);
return wrapper;
}
}
实体类
@Data
public class Test {
private String id;
private String name;
}
|