restTemplate有很多方法,有postForEntity(...)、getForEntity(...),我个人建议使用exchange(),因为exchange()是通用方法,既可以做GET、POST请求,还可以设置其他请求方式。
1、(POST)如果服务端代码是下面这样子,使用了@RequestBody来修饰参数,那么客户端就需要把请求头设置为content-type=application/json;charset=utf-8;
服务器端代码:
@RequestMapping(value = "HP3",method = RequestMethod.POST)
@ResponseBody
public User HP3(@RequestBody User user,HttpServletRequest request) throws Exception{
HttpSession session = request.getSession();
String s=session.getId();
System.out.println("---hello-----");
System.out.println(user);
return user;
}
客户端调用代码:
try{
RestTemplate restTemplate=new RestTemplate();
HttpHeaders httpHeaders=new HttpHeaders();
httpHeaders.add("content-type","application/json; charset=UTF-8");
httpHeaders.add("accept","application/json, text/plain, */*");
//当接口参数被@RequestBody修饰的时候,可以使用HashMap,实体类等传递参数
HashMap<String, String> params = new HashMap<>(); //这里可以使用hashMap 也可以使用LinkedMultiValueMap
params.put("username", "hp");
params.put("userage", "23");
//转为JSON格式
String jsonRequestStr=JSON.toJSONString(params);
HttpEntity httpEntity=new HttpEntity(jsonRequestStr,httpHeaders);
ResponseEntity responseEntity=restTemplate.exchange("http://localhost:8090/HP3",HttpMethod.POST,httpEntity,String.class);
System.out.println("test over");
}catch (Exception e){
}
2、(POST)如果服务端代码是下面这样子,没有使用@RequestBody来修饰参数,那么客户端就需要把请求头设置为content-type=application/x-www-form-urlencoded; charset=UTF-8
服务器端代码:
@RequestMapping(value = "HP2",method = RequestMethod.POST)
@ResponseBody
public User HP2(User user) throws Exception{
System.out.println("---hello-----");
System.out.println(user);
return user;
}
客户端调用代码:
try{
RestTemplate restTemplate=new RestTemplate();
HttpHeaders httpHeaders=new HttpHeaders();
httpHeaders.add("content-type","application/x-www-form-urlencoded;charset=UTF-8");
httpHeaders.add("accept","application/json, text/plain, */*");
//当接口参数没有被@RequestBody修饰的时候,使用LinkedMultiValueMap传递参数
MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); //这里不能使用hashMap,必须使用LinkedMultiValueMap,切记
params.set("username", "hp");
params.set("userage", "23");
HttpEntity httpEntity=new HttpEntity(params,httpHeaders);
ResponseEntity responseEntity=restTemplate.exchange("http://localhost:8090/HP2",HttpMethod.POST,httpEntity,String.class);
System.out.println("test over");
}catch (Exception e){
}
3、(GET)如果是GET请求,get请求是没有请求体的,所以像上面那样子设置请求体 HttpEntity httpEntity=new HttpEntity(params,httpHeaders);就可以设置为HttpEntity httpEntity=new HttpEntity("",httpHeaders);消息头可以通过httpHeaders设置。
服务器端代码:
@RequestMapping(value = "HPX11",method = RequestMethod.GET)
@ResponseBody
public User HPX11(HttpServletRequest request, HttpServletResponse response, User user) throws Exception{
return user;
}
客户端代码:
try{
RestTemplate restTemplate=new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/json, text/plain, */*");
headers.add("Content-Type", "application/json; charset=UTF-8"); //get可以不设置这里,设置了也没问题
HttpEntity httpEntity = new HttpEntity("", headers);
//解决乱码问题
restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
ResponseEntity<String> responseEntity=restTemplate.exchange("http://localhost:8090/HPX11?username='zs'",HttpMethod.GET,httpEntity,String.class);
System.out.println(responseEntity.getBody());
System.out.println("test over");
}catch (Exception e){
}