使用RestTemplate模板调用接口

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){

}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值