RestTemplate提交表单数据的三种方法

 

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

Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 405 Method Not Allowed
    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.exchange(RestTemplate.java:531)


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

String url = "http://localhost/mirana-ee/app/login";
RestTemplate client = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
//  请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//  封装参数,千万不要替换为Map与HashMap,否则参数无法传递
MultiValueMap<String, String> params= new LinkedMultiValueMap<String, String>();
//  也支持中文
params.add("username", "用户名");
params.add("password", "123456");
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);
//  执行HTTP请求
ResponseEntity<String> response = client.exchange(url, HttpMethod.POST, requestEntity, String.class);
//  输出结果
System.out.println(response.getBody());

 


2. 用postForEntity进行提交
postForEntity是对exchange的简化,仅仅只需要减少HttpMethod.POST参数,如下:

//  上面的代码完全一样
//  仅需替换exchange方法
ResponseEntity<String> 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 Account account)
public Account getAccount(@RequestBody Account account) {
    account.setVersion(new Date());
    return account;
}


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

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

//  请求地址
String url = "http://localhost/mirana-ee/app/login";
RestTemplate client = new RestTemplate();
//  一定要设置header
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
//  将提交的数据转换为String
//  最好通过bean注入的方式获取ObjectMapper
ObjectMapper mapper = new ObjectMapper();
Map<String, String> params= Maps.newHashMap();
params.put("username", "国米");
params.put("password", "123456");
String value = mapper.writeValueAsString(params);
HttpEntity<String> requestEntity = new HttpEntity<String>(value, headers);
//  执行HTTP请求
ResponseEntity<String> response = client.postForEntity(url, requestEntity , String.class );
System.out.println(response.getBody());


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

Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 400 Bad 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是无法获取到请求参数,如将上面服务端的代码改为如下格式,则肯定得不到数据,但表单提交则相反。

@RequestMapping(value="/login", consumes="application/json", method=RequestMethod.POST)
public Account getAccount(@RequestBody Account account, HttpServletRequest request) {
    //  肯定得不到参数值
    System.out.println(request.getParameter("username"));
    account.setVersion(new Date());
    return account;
}


4. HttpEntity的结构
HttpEntity是对HTTP请求的封装,包含两部分,header与body,header用于设置请求头,而body则用于设置请求体,所以其的构造器如下:

//  value为请求体
//  header为请求头
HttpEntity<String> requestEntity = new HttpEntity<String>(value, headers);


5. HttpEntity与uriVariables
在RestTemplate的使用中,HttpEntity用于传递具体的参数值,而uriVariables则用于格式化Http地址,而不是地址参数,正确的用法如下:

//  在地址中加入格式化参数path
String url = "http://localhost/mirana-ee/app/{path}";
//  准备格式化参数
Map<String, String> varParams = Maps.newHashMap();
varParams.put("path", "login");
//  其他代码略
//  格式化提交地址
ResponseEntity<String> response = client.postForEntity(url, requestEntity , String.class, varParams);


6. 关于HttpMessageConverter的说明
在网上的很多例子中,我发现很多人为了处理Payload提交,都添加了自定义的HttpMessageConverter,如下:

//  完全没有必要
client.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
client.getMessageConverters().add(new StringHttpMessageConverter());


然后,经过我查看源码与调试发现,RestTemplate内置了7种HttpMessageConverter,如下:
1. org.springframework.http.converter.ByteArrayHttpMessageConverter
2. org.springframework.http.converter.StringHttpMessageConverter
3. org.springframework.http.converter.ResourceHttpMessageConverter
4. org.springframework.http.converter.xml.SourceHttpMessageConverter
5. org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter
6. org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter
7. org.springframework.http.converter.json.MappingJackson2HttpMessageConverter

 

 

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

 

 

 

 


原文链接:https://blog.csdn.net/yiifaa/article/details/77939282

posted @ 2020-04-22 14:40  panchanggui  阅读(2929)  评论(0编辑  收藏  举报