美文网首页
解决restTemplate的各种调用问题

解决restTemplate的各种调用问题

作者: 倪宝华 | 来源:发表于2017-10-13 11:08 被阅读0次

                 最近在spring boot项目中REST接口的开发中使用restTemplate踩了一些坑,其实是自己对提交的各种方式有点混淆,现在梳理出来供大家使用避免再次踩坑。

                 利用RestTemplate进行接口测试是种常见的方法,但在使用过程中,由于其方法参数众多,很多同学又混淆了表单提交与Payload提交方式的差别,而且接口设计与传统的浏览器使用的提交方式又有差异,经常出现各种各样的错误,如405错误,或者根本就得不到提交的数据。


    1. 用exchange方法提交

    exchange既可以执行POST方法,还可以执行GET,所以应用最为广泛,使用方法如下:

          String url ="http://localhost/mirana-ee/app/login";

          RestTemplate client =newRestTemplate();

          HttpHeaders headers =newHttpHeaders();//  请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交         headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);//  封装参数,千万不要替换为Map与HashMap,否则参数无法传递

         MultiValueMap params=newLinkedMultiValueMap();//  也支持中文params.add("username","用户名");

          params.add("password","123456");

         HttpEntity> requestEntity =newHttpEntity>(params, headers);//  执行HTTP请求

          ResponseEntity response = client.exchange(url, HttpMethod.POST, requestEntity, String.class);//  输出结果

          System.out.println(response.getBody());


    2、用postForEntity进行提交

    postForEntity是对exchange的简化,仅仅只需要减少HttpMethod.POST参数,如下:

    //  上面的代码完全一样//  仅需替换exchange方法

    ResponseEntity response = client.postForEntity(url, requestEntity , String.class );


    3. 关于表单提交与Payload提交的差异

    在Controller的方法参数中,如果将“@ModelAttribute”改为“@RequestBody”注解,则此时的提交方式为Payload方式提交,详细的差异请参见《 $.ajax使用总结(一):Form提交与Payload提交》,代码示例如下:

    //  请注意@RequestBody注解

        @RequestMapping(value="/login", method=RequestMethod.POST, consumes="application/json")

          //  千万不要画蛇添足添加@ModelAttribute,否则会被其覆盖,如下

            //  public Account getAccount(@RequestBody@ModelAttribute Accountccount)

                 publicAccountgetAccount(@RequestBody Account account) {   

                              account.setVersion(newDate());

                               return   account;

                  }

    再次强调一次,千万不要画蛇添足再次添加“@ModelAttribute”,因为其优先级比较高,所以系统会采用表单方式解析提交内容。

    对于Payload方式,提交的内容一定要是String,且Header要设置为“application/json”,示例如下:

          //  请求地址

         String url ="http://localhost/mirana-ee/app/login";RestTemplate client =newRestTemplate();

         //  一定要设置header 

         HttpHeaders headers =newHttpHeaders();

         headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

         //  将提交的数据转换为String

         //  最好通过bean注入的方式获取ObjectMapper

         ObjectMapper mapper =newObjectMapper();

         Map params= Maps.newHashMap();

          params.put("username","国米");

          params.put("password","123456");

           String value = mapper.writeValueAsString(params);

           HttpEntity requestEntity =newHttpEntity(value, headers);

           //  执行HTTP请求

           ResponseEntity response = client.postForEntity(url, requestEntity , String.class );

           System.out.println(response.getBody());

    如果内容不是以String方式提交,那么一定会出现以下错误:

    Exceptioninthread"main"org.springframework.web.client.HttpClientErrorException:400Bad Request    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:63)    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)    at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:407)



    最后需要强调的是,通过@RequestBody是无法获取到请求参数,如将上面服务端的代码改为如下格式,则肯定得不到数据,但表单提交则相反。


    结论

    RestTemplate能大幅简化了提交表单数据的难度,并且附带了自动转换JSON数据的功能,但只有理解了HttpEntity的组成结构(header与body),且理解了与uriVariables之间的差异,才能真正掌握其用法。

    相关文章

      网友评论

          本文标题:解决restTemplate的各种调用问题

          本文链接:https://www.haomeiwen.com/subject/dixzyxtx.html