resttemplate exchange方法,两种用法结果不一样
有问题的代码
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.util.Arrays;
/**
* <p>Title:</p>
* <p>Description:</p>
*
* @author zhanghong
* @date 2023/9/27
*/
public class Test {
public static void main(String[] args) throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.set("appkey", "c9406731-50bc-4e23-b3f5-b7e7d3093875");
headers.set("sign", "OhnuJEJU/SVCvf4oZhOzRQ==");
headers.setContentType(MediaType.APPLICATION_PDF);
headers.setAccept(Arrays.asList(MediaType.ALL));
headers.set("time", "1695776131807");
String url = "http://220.191.224.202:28800/PrintServer/api/PrintWord?groupGuid=%7BC05481E9-4FD1-4A6E-B6E1-D100CF1377EA%7D&toRowGuid=100000000015&dataGuid=01103ebf-a884-4282-b996-78dd64196946";
HttpEntity httpEntity = new HttpEntity<>( headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<byte[]> exchange1 = restTemplate.exchange(url, HttpMethod.GET, httpEntity, byte[].class);
byte[] body1 = exchange1.getBody();
URI uri =new URI(url);
RequestEntity<Void> requestEntity =
RequestEntity.get(uri)
.header("appkey", "c9406731-50bc-4e23-b3f5-b7e7d3093875")
.header("sign", "OhnuJEJU/SVCvf4oZhOzRQ==")
.header("time", "1695776131807")
.build();
ResponseEntity<byte[]> exchange2 = restTemplate.exchange(requestEntity, byte[].class);
byte[] body2 = exchange2.getBody();
}
}
上面的代码,exchange1报错,exchagne2正常获取数据,
原因是exchange1对url做了二次编码,而exchagne2没有
解决方案:
String url = "http://220.191.224.202:28800/PrintServer/api/PrintWord?groupGuid=%7BC05481E9-4FD1-4A6E-B6E1-D100CF1377EA%7D&toRowGuid=100000000015&dataGuid=01103ebf-a884-4282-b996-78dd64196946";
RestTemplate restTemplate = new RestTemplate();
String baseUrl = "http://220.191.224.202:28800/PrintServer/api/PrintWord";
// 占位符的值已经正确编码,不需要手动编码
String groupGuid = "{C05481E9-4FD1-4A6E-B6E1-D100CF1377EA}";
String toRowGuid = "100000000015";
String dataGuid = "01103ebf-a884-4282-b996-78dd64196946";
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl)
.queryParam("groupGuid", groupGuid)
.queryParam("toRowGuid", toRowGuid)
.queryParam("dataGuid", dataGuid);
URI uri = builder.build().toUri();
HttpHeaders headers = new HttpHeaders();
headers.set("appkey", "c9406731-50bc-4e23-b3f5-b7e7d3093875");
headers.set("sign", "OhnuJEJU/SVCvf4oZhOzRQ==");
headers.set("Accept","application/octet-stream, application/json, application/*+json, */*");
headers.set("time", "1695776131807");
HttpEntity httpEntity = new HttpEntity<>( headers);
ResponseEntity<byte[]> exchange1 = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, byte[].class);
byte[] body1 = exchange1.getBody();
URI uri2 =new URI(url);
RequestEntity<Void> requestEntity =
RequestEntity.get(uri2)
.header("appkey", "c9406731-50bc-4e23-b3f5-b7e7d3093875")
.header("sign", "OhnuJEJU/SVCvf4oZhOzRQ==")
.header("time", "1695776131807")
.build();
ResponseEntity<byte[]> exchange2 = restTemplate.exchange(requestEntity, byte[].class);
byte[] body2 = exchange2.getBody();