发送GET请求
<T> ResponseEntity<T> getForEntity(URI var1, Class<T> var2)
/*client*/
@Test
public void testRest(){
RestTemplate restTemplate = new RestTemplate();
String getUrl = "http://localhost:8083/mock/test/get" + "/" + "helloworld";
ResponseEntity<Student> entity = restTemplate.getForEntity(getUrl,Student.class);
log.info(entity.toString());
log.info(entity.getHeaders().toString());
log.info(entity.getBody().toString());
}
客户端输出日志:
2020-05-27 09:14:44 [main] INFO AppTest -<200,Student(name=王杰, age=28),[Content-Type:"application/json", Transfer-Encoding:"chunked", Date:"Wed, 27 May 2020 01:14:44 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]>
2020-05-27 09:14:44 [main] INFO AppTest -[Content-Type:"application/json", Transfer-Encoding:"chunked", Date:"Wed, 27 May 2020 01:14:44 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]
2020-05-27 09:14:44 [main] INFO AppTest -Student(name=王杰, age=28)
/*server*/
@RestController
@Slf4j
public class Demo {
@GetMapping("/test/get/{name}")
public JSONObject get(@PathVariable("name") String username){
log.debug("get server被调用");
log.info(username);
JSONObject result = new JSONObject();
result.put("name","王杰");
result.put("age",28);
return result;
}
}
服务端输出日志:
2020-05-27 09:14:44 [http-nio-8083-exec-3] DEBUG com.ai.mock.rests.Demo -get server被调用
2020-05-27 09:14:44 [http-nio-8083-exec-3] INFO com.ai.mock.rests.Demo -helloworld
<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables)
/*client*/
@Test
public void testRest2(){
RestTemplate restTemplate = new RestTemplate();
String getUrl = "http://localhost:8083/mock/test/get2?name={name}&age={age}";
Map<String,Object> map = new HashMap<>();
map.put("name","皮皮虾");
map.put("age",3);
ResponseEntity<Student> entity = restTemplate.getForEntity(getUrl,Student.class,map);
log.info(entity.toString());
log.info(entity.getHeaders().toString());
log.info(entity.getBody().toString());
/*验证entity中放的就是Student对象*/
Student s = entity.getBody();
log.info(s.getName());
log.info(s.getAge().toString());
}
客户端输出日志:
2020-05-27 09:26:34 [main] INFO AppTest -<200,Student(name=王杰, age=28),[Content-Type:"application/json", Transfer-Encoding:"chunked", Date:"Wed, 27 May 2020 01:26:34 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]>
2020-05-27 09:26:34 [main] INFO AppTest -[Content-Type:"application/json", Transfer-Encoding:"chunked", Date:"Wed, 27 May 2020 01:26:34 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]
2020-05-27 09:26:34 [main] INFO AppTest -Student(name=王杰, age=28)
2020-05-27 09:26:34 [main] INFO AppTest -王杰
2020-05-27 09:26:34 [main] INFO AppTest -28
/*server*/
@GetMapping("/test/get2")
public JSONObject get2(@RequestParam("name") String username,@RequestParam("age") Integer age){
log.debug("get server被调用");
log.info("username=" + username);
log.info("age=" + age);
JSONObject result = new JSONObject();
result.put("name","王杰");
result.put("age",28);
return result;
}
服务端输出日志:
2020-05-27 09:26:34 [http-nio-8083-exec-2] DEBUG com.ai.mock.rests.Demo -get server被调用
2020-05-27 09:26:34 [http-nio-8083-exec-2] INFO com.ai.mock.rests.Demo -username=皮皮虾
2020-05-27 09:26:34 [http-nio-8083-exec-2] INFO com.ai.mock.rests.Demo -age=3
<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables)
/*client*/
@Test
public void testRest3(){
RestTemplate restTemplate = new RestTemplate();
String getUrl = "http://localhost:8083/mock/test/get3?name={name}";
String name = "hello world";
ResponseEntity<Student> entity = restTemplate.getForEntity(getUrl,Student.class,name);
Student s = entity.getBody();
log.info(s.getName());
log.info(entity.toString());
log.info(entity.getHeaders().toString());
log.info(entity.getBody().toString());
}
客户端日志输出:
2020-05-27 09:39:54 [main] INFO AppTest -<200,Student(name=王杰, age=28),[Content-Type:"application/json", Transfer-Encoding:"chunked", Date:"Wed, 27 May 2020 01:39:54 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]>
2020-05-27 09:39:54 [main] INFO AppTest -[Content-Type:"application/json", Transfer-Encoding:"chunked", Date:"Wed, 27 May 2020 01:39:54 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]
2020-05-27 09:39:54 [main] INFO AppTest -Student(name=王杰, age=28)
/*server*/
@GetMapping("/test/get3")
public JSONObject get3(@RequestParam("name") String username){
log.debug("get server被调用");
log.info(username);
JSONObject result = new JSONObject();
result.put("name","王杰");
result.put("age",28);
return result;
}
服务端日志输出:
2020-05-27 09:39:54 [http-nio-8083-exec-10] DEBUG com.ai.mock.rests.Demo -get server被调用
2020-05-27 09:39:54 [http-nio-8083-exec-10] INFO com.ai.mock.rests.Demo -hello world
如果Object为一个java对象,想像Map一样传参,行不通!在这里我特么想极力吐槽一波。如果想传java对象,得这样传:
/*client*/
@Test
public void testRest4(){
RestTemplate restTemplate = new RestTemplate();
String getUrl = "http://localhost:8083/mock/test/get4?name={student}";
Student student = new Student("皮皮虾",3);
ResponseEntity<Student> entity = restTemplate.getForEntity(getUrl,Student.class,student);
log.info(entity.toString());
log.info(entity.getHeaders().toString());
log.info(entity.getBody().toString());
}
客户端日志输出:
2020-05-27 09:48:35 [main] INFO AppTest -<200,Student(name=王杰, age=28),[Content-Type:"application/json", Transfer-Encoding:"chunked", Date:"Wed, 27 May 2020 01:48:34 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]>
2020-05-27 09:48:35 [main] INFO AppTest -[Content-Type:"application/json", Transfer-Encoding:"chunked", Date:"Wed, 27 May 2020 01:48:34 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]
2020-05-27 09:48:35 [main] INFO AppTest -Student(name=王杰, age=28)
/*server*/
@GetMapping("/test/get4")
public JSONObject get4(@RequestParam("name") String name){
log.debug("get server被调用");
log.info(name);
JSONObject result = new JSONObject();
result.put("name","王杰");
result.put("age",28);
return result;
}
服务端日志输出:
2020-05-27 09:48:34 [http-nio-8083-exec-5] DEBUG com.ai.mock.rests.Demo -get server被调用
2020-05-27 09:48:34 [http-nio-8083-exec-5] INFO com.ai.mock.rests.Demo -Student(name=皮皮虾, age=3)
<T> T getForObject(URI url, Class<T> responseType)
/*client*/
@Test
public void testRest5(){
RestTemplate restTemplate = new RestTemplate();
String getUrl = "http://localhost:8083/mock/test/get5/helloworld";
Student stu = restTemplate.getForObject(getUrl,Student.class);
log.info(stu.toString());
}
客户端输出日志:
2020-05-27 10:10:52 [main] INFO AppTest -Student(name=王杰, age=28)
/*server*/
@GetMapping("/test/get5/{name}")
public JSONObject get5(@PathVariable("name") String username){
log.debug("get server被调用");
log.info(username);
JSONObject result = new JSONObject();
result.put("name","王杰");
result.put("age",28);
return result;
}
服务端输出日志:
2020-05-27 10:10:52 [http-nio-8083-exec-1] DEBUG com.ai.mock.rests.Demo -get server被调用
2020-05-27 10:10:52 [http-nio-8083-exec-1] INFO com.ai.mock.rests.Demo -helloworld
/*client*/
@Test
public void testRest6(){
RestTemplate restTemplate = new RestTemplate();
String getUrl = "http://localhost:8083/mock/test/get6?name={name}&age={age}";
Map<String,Object> map = new HashMap<>();
map.put("name","皮皮虾");
map.put("age",3);
Student s = restTemplate.getForObject(getUrl,Student.class,map);
log.info(s.toString());
}
客户端输出日志:
2020-05-27 10:14:47 [main] INFO AppTest -Student(name=王杰, age=28)
/*server*/
@GetMapping("/test/get6")
public JSONObject get6(@RequestParam("name") String username,@RequestParam("age") Integer age){
log.debug("get server被调用");
log.info("username=" + username);
log.info("age=" + age);
JSONObject result = new JSONObject();
result.put("name","王杰");
result.put("age",28);
return result;
}
服务端输出日志:
2020-05-27 10:14:47 [http-nio-8083-exec-2] DEBUG com.ai.mock.rests.Demo -get server被调用
2020-05-27 10:14:47 [http-nio-8083-exec-2] INFO com.ai.mock.rests.Demo -username=皮皮虾
2020-05-27 10:14:47 [http-nio-8083-exec-2] INFO com.ai.mock.rests.Demo -age=3
<T> T getForObject(String url, Class<T> responseType, Object... uriVariables)
/*client*/
@Test
public void testRest7(){
RestTemplate restTemplate = new RestTemplate();
String getUrl = "http://localhost:8083/mock/test/get7?name={stu}";
Student stu = new Student("皮皮虾",3);
Student s = restTemplate.getForObject(getUrl,Student.class,stu);
log.info(s.toString());
}
客户端日志输出:
2020-05-27 10:22:29 [main] INFO AppTest -Student(name=王杰, age=28)
/*server*/
@GetMapping("/test/get7")
public JSONObject get7(@RequestParam("name") String name){
log.debug("get server被调用");
log.info(name);
JSONObject result = new JSONObject();
result.put("name","王杰");
result.put("age",28);
return result;
}
服务端日志输出:
2020-05-27 10:25:50 [http-nio-8083-exec-3] DEBUG com.ai.mock.rests.Demo -get server被调用
2020-05-27 10:25:50 [http-nio-8083-exec-3] INFO com.ai.mock.rests.Demo -Student(name=皮皮虾, age=3)
GET小结
看了前面的一堆测试代码,不禁提问:你怎么这么唇笔啊?server端不会用JSONObject来接受MAP 或者 java对象吗?
答案是:不可以!特么的设计者的思想就是GET方式 传参 就从URL里面取值。不管你是从path还是从url参数里面都可以。就是不可以从body里面取。
getForEntity与getForObject的区别是 getForEntity返回的数据中包含了请求头、请求体,而getForObject只有体。
发送POST请求
POST提交方式主要有两种:form表单提交、payload提交。
这里给出鄙人的一点浅薄的见解:
- form表单提交:无论是后端通过restTemplate还是前端人员通过ajax发送的请求,请求头中Content-Type: application/x-www-form-urlencoded; charset=UTF-8。server端获取参数值,需要使用RequestParmas,不能使用RequestBody获取到。
- payload提交:无论是后端通过restTemplate还是前端人员通过ajax发送的请求,请求头中Content-Type: application/json;charset=UTF-8。server端获取参数值,需要使用RequestBody,不能使用RequestParams获取到。
<T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType)
<T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType) form提交
/*client*/
/*表单提交 Form Data服务端只能用RequestParams取值 不能用RequestBody*/
/*Content-Type: application/x-www-form-urlencoded; charset=UTF-8*/
@Test
public void testRest8(){
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8083/mock/test/post";
MultiValueMap params = new LinkedMultiValueMap();
params.add("name","皮皮虾");
params.add("age",3);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap> entity = new HttpEntity<>(params,headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url,entity,String.class);
log.info(responseEntity.getBody());
}
客户端日志:
2020-05-28 16:06:54 [main] INFO AppTest -{"name":"王杰","age":28}
/*server*/
@PostMapping("/test/post")
public JSONObject post(@RequestParam("name") String name,@RequestParam("age") Integer age){
log.debug("post server被调用");
log.info(name);
log.info(age.toString());
JSONObject result = new JSONObject();
result.put("name","王杰");
result.put("age",28);
return result;
}
服务端日志:
2020-05-28 16:06:54 [http-nio-8083-exec-4] DEBUG com.ai.mock.rests.Demo -post server被调用
2020-05-28 16:06:54 [http-nio-8083-exec-4] INFO com.ai.mock.rests.Demo -皮皮虾
2020-05-28 16:06:54 [http-nio-8083-exec-4] INFO com.ai.mock.rests.Demo -3
<T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType) payload提交
/*<T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType)*/
/*payload提交*/
/*参数放在body requestParams 取不到*/
/*Content-Type: application/json;charset=UTF-8*/
@Test
public void testRest9(){
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8083/mock/test/post2";
MultiValueMap params = new LinkedMultiValueMap();
params.add("name","皮皮虾");
params.add("age",3);
/*此处注销的代码块 也可以作为请求体参数传递*/
/*Map<String,Object> params = new HashMap<>();
params.put("name","皮皮虾");
params.put("age",3);*/
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<Map> entity = new HttpEntity<>(params,headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url,entity,String.class);
log.info(responseEntity.getBody());
}
客户端日志:
2020-05-28 16:12:35 [main] INFO AppTest -{"name":"王杰","age":28}
/*server*/
@PostMapping("/test/post2")
public JSONObject post2(@RequestBody JSONObject jsonObject){
log.debug("post server被调用");
log.info(jsonObject.toJSONString());
JSONObject result = new JSONObject();
result.put("name","王杰");
result.put("age",28);
return result;
}
服务端日志:
2020-05-28 16:12:35 [http-nio-8083-exec-6] DEBUG com.ai.mock.rests.Demo -post server被调用
2020-05-28 16:12:35 [http-nio-8083-exec-6] INFO com.ai.mock.rests.Demo -{"name":["皮皮虾"],"age":[3]}
<T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables)
这里就基于payload方式举例子了,也可以基于form data 方式。
/*除了请求头里面传值 还可以在 请求参数里面传值 需要显示申明 请求体用RequestBody获取 请求头用RequestParam获取*/
@Test
public void testRest10(){
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8083/mock/test/post3?sex={sex}";
MultiValueMap params = new LinkedMultiValueMap();
params.add("name","皮皮虾");
params.add("age",3);
Map<String,Object> uriParam = new HashMap<>();
uriParam.put("sex","man");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<Map> entity = new HttpEntity<>(params,headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url,entity,String.class,uriParam);
log.info(responseEntity.getBody());
}
客户端日志:
2020-05-28 16:18:32 [main] INFO AppTest -{"name":"王杰","age":28}
@PostMapping("/test/post3")
public JSONObject post2(@RequestBody JSONObject jsonObject,@RequestParam("sex") String sex){
log.debug("post server被调用");
log.info(jsonObject.toJSONString());
log.info(sex);
JSONObject result = new JSONObject();
result.put("name","王杰");
result.put("age",28);
return result;
}
server端日志:
2020-05-28 16:18:32 [http-nio-8083-exec-7] DEBUG com.ai.mock.rests.Demo -post server被调用
2020-05-28 16:18:32 [http-nio-8083-exec-7] INFO com.ai.mock.rests.Demo -{"name":["皮皮虾"],"age":[3]}
2020-05-28 16:18:32 [http-nio-8083-exec-7] INFO com.ai.mock.rests.Demo -man
<T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables)
这里就基于payload方式举例子了,也可以基于form data 方式。
@Test
public void testRest11(){
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8083/mock/test/post4?stu={stu}";
MultiValueMap params = new LinkedMultiValueMap();
params.add("name","皮皮虾");
params.add("age",3);
Student stu = new Student("王杰",20);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<Map> entity = new HttpEntity<>(params,headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url,entity,String.class,stu);
log.info(responseEntity.getBody());
}
客户端日志:
2020-05-28 16:22:16 [main] INFO AppTest -{"name":"王杰","age":28}
@PostMapping("/test/post4")
public JSONObject post4(@RequestBody JSONObject jsonObject,@RequestParam("stu") String stu){
log.debug("post server被调用");
log.info(jsonObject.toJSONString());
log.info(stu);
JSONObject result = new JSONObject();
result.put("name","王杰");
result.put("age",28);
return result;
}
服务端日志:
2020-05-28 16:22:16 [http-nio-8083-exec-10] DEBUG com.ai.mock.rests.Demo -post server被调用
2020-05-28 16:22:16 [http-nio-8083-exec-10] INFO com.ai.mock.rests.Demo -{"name":["皮皮虾"],"age":[3]}
2020-05-28 16:22:16 [http-nio-8083-exec-10] INFO com.ai.mock.rests.Demo -Student(name=王杰, age=20)
postForEntity小结:如果是form data请求 需要设置请求头的ContentType为MediaType.APPLICATION_JSON_UTF8 且将参数 放入MultiValueMap中 不可以使用普通Map,同时需要将请求体 请求头放入HttpEntity中之后,发起请求。服务端接受参数使用RequestParam。
如果是payload请求 普通Map也可以,需要将CententType设置为MediaType.APPLICATION_JSON_UTF8。服务端接受参数使用ReuqestBody.
<T> T postForObject(URI url, @Nullable Object request, Class<T> responseType)
/*默认 payload方式*/
@Test
public void testRest12(){
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8083/mock/test/post2";
Map<String,Object> params = new HashMap<>();
params.put("name","皮皮虾");
params.put("age",3);
String result = restTemplate.postForObject(url,params,String.class);
log.info(result);
}
客户端日志:
2020-05-28 16:26:08 [main] INFO AppTest -{"name":"王杰","age":28}
@PostMapping("/test/post2")
public JSONObject post2(@RequestBody JSONObject jsonObject){
log.debug("post server被调用");
log.info(jsonObject.toJSONString());
JSONObject result = new JSONObject();
result.put("name","王杰");
result.put("age",28);
return result;
}
服务端日志:
2020-05-28 16:26:08 [http-nio-8083-exec-2] DEBUG com.ai.mock.rests.Demo -post server被调用
2020-05-28 16:26:08 [http-nio-8083-exec-2] INFO com.ai.mock.rests.Demo -{"name":"皮皮虾","age":3}
<T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables)
@Test
public void testRest13(){
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8083/mock/test/post3?sex={sex}";
Map<String,Object> params = new HashMap<>();
params.put("name","皮皮虾");
params.put("age",3);
Map<String,Object> uriParam = new HashMap<>();
uriParam.put("sex","man");
String result = restTemplate.postForObject(url,params,String.class,uriParam);
log.info(result);
}
客户端日志:
2020-05-28 16:28:51 [main] INFO AppTest -{"name":"王杰","age":28}
@PostMapping("/test/post3")
public JSONObject post2(@RequestBody JSONObject jsonObject,@RequestParam("sex") String sex){
log.debug("post server被调用");
log.info(jsonObject.toJSONString());
log.info(sex);
JSONObject result = new JSONObject();
result.put("name","王杰");
result.put("age",28);
return result;
}
服务端日志:
2020-05-28 16:28:51 [http-nio-8083-exec-4] DEBUG com.ai.mock.rests.Demo -post server被调用
2020-05-28 16:28:51 [http-nio-8083-exec-4] INFO com.ai.mock.rests.Demo -{"name":"皮皮虾","age":3}
2020-05-28 16:28:51 [http-nio-8083-exec-4] INFO com.ai.mock.rests.Demo -man
<T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables)
@Test
public void testRest14(){
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8083/mock/test/post4?stu={stu}";
Map<String,Object> params = new HashMap<>();
params.put("name","皮皮虾");
params.put("age",3);
Student stu = new Student("王杰",20);
String result = restTemplate.postForObject(url,params,String.class,stu);
log.info(result);
}
客户端日志:
2020-05-28 16:29:42 [main] INFO AppTest -{"name":"王杰","age":28}
@PostMapping("/test/post4")
public JSONObject post4(@RequestBody JSONObject jsonObject,@RequestParam("stu") String stu){
log.debug("post server被调用");
log.info(jsonObject.toJSONString());
log.info(stu);
JSONObject result = new JSONObject();
result.put("name","王杰");
result.put("age",28);
return result;
}
服务端日志:
2020-05-28 16:29:42 [http-nio-8083-exec-6] DEBUG com.ai.mock.rests.Demo -post server被调用
2020-05-28 16:29:42 [http-nio-8083-exec-6] INFO com.ai.mock.rests.Demo -{"name":"皮皮虾","age":3}
2020-05-28 16:29:42 [http-nio-8083-exec-6] INFO com.ai.mock.rests.Demo -Student(name=王杰, age=20)
exchange
至于exchange如果知道上面的用法之后 ,只需要在参数请求方法中 添加一个HttpMethod就OK。不再赘述。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。