RestTemplate 发送Post 多个参数请求

时间:2022-07-31 19:51:08
          MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
requestEntity.add("clientFlag", clientFlag);
requestEntity.add("xml", xml);
requestEntity.add("verifyData", strMd5);
         String s = REST_TEMPLATE.postForObject("http://10.10.129.19/svsr/Receive.asmx/OrderXML", requestEntity, String.class);

!!

    最直接的方法就是 写个类吧!!

可惜了 JAVA 没有  c# 中  匿名类 这个东西啊

  var news = new { title="特大喜讯",author="夕阳眼",postdate="3013-10-9",msg="今晚公布"};

补充:

  设置请求头:

  

        MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
postParameters.add("userCode", "291974");
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/x-www-form-urlencoded");
HttpEntity<MultiValueMap<String, Object>> r = new HttpEntity<>(postParameters, headers); String data= restTemplate.postForObject("http://10.10.12.27:9000/Criteria", r, String.class);
System.out.println(data);
注意:
    RestTemplate 会对请求头判断,会更具请求头不通走不同的逻辑。默认是 text/html /*
      如果是  application/x-www-form-urlencoded  这个请求头  会对数据镜像 url 编码。
    
    不可以传递 非 字符串类型的数据!!
关于  HttpEntity  这个对象的一点说明

  
HttpEntity  就是存放 两个字段数据  一个是请求数据  一个是请求头!  从定义上就可以看到   虽然可以 POST 等 提交from  数据  但是好是推荐使用实体类型来传递 HTTP 请求数据。
public class HttpEntity<T> {
private final HttpHeaders headers;
private final T body; public HttpEntity(T body, MultiValueMap<String, String> headers) {
this.body = body;
HttpHeaders tempHeaders = new HttpHeaders();
if (headers != null) {
tempHeaders.putAll(headers);
}
this.headers = HttpHeaders.readOnlyHttpHeaders(tempHeaders);
}
}