当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Boot RestTemplate提交表单数据的三种方法

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

在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方式提交,代码示例如下:

// 请注意@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之间的差异,才能真正掌握其用法。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网