两种方法
使用注入的方式
@Autowired
RestTemplate restTemplate;
@Test
public void getTest(){
//设置get请求参数
Map<String,String> params = new HashMap<>();
params.put("orgCode","900000");
params.put("waybillNo","773011197999686");
//设置请求头参数
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("appId", "app_service");
HttpEntity request = new HttpEntity(requestHeaders);
ResponseEntity<String> exchange = restTemplate.exchange(INTERCEPT_URL + "?orgCode={orgCode}&waybillNo={waybillNo}", HttpMethod.GET,request, String.class, params);
System.out.println(exchange.getBody());
/**
* 也可以使用拼接参数的方式
*/
String orgCode = "900000";
String waybillNo = "773011197999686";
ResponseEntity<String> anotherExchange = restTemplate.exchange(INTERCEPT_URL + "?orgCode="+orgCode+"&waybillNo="+waybillNo, HttpMethod.GET,request, String.class);
System.out.println(anotherExchange.getBody());
}
使用new RestTemplate()的方式
依赖信息
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.1.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
spring-web 版本选择
较低版本中的org.springframework.web.client.RestTemplate类中:
① httpEntityCallback(@Nullable Object requestBody, Type responseType)
② responseEntityExtractor(Type responseType)
是用protected修饰的,不方便调用
spring-core 版本选择
spring-web是有spring-core依赖的,但是项目里其他依赖也可能加载spring-core,建议直接指定一个spring-core版本。如果spring-core版本较低,低版本中不存在org.springframework.core.log.LogDelegateFactory类,在new RestTemplate()时会报错:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/log/LogDelegateFactory
at org.springframework.http.HttpLogging.forLog(HttpLogging.java:68)
at org.springframework.http.HttpLogging.forLogName(HttpLogging.java:57)
at org.springframework.http.client.support.HttpAccessor.<init>(HttpAccessor.java:54)
at org.springframework.http.client.support.InterceptingHttpAccessor.<init>(InterceptingHttpAccessor.java:44)
at org.springframework.web.client.RestTemplate.<init>(RestTemplate.java:140)
at com.cainiao.spt.plugin.InterceptOnlineCheck.main(InterceptOnlineCheck.java:132)
Caused by: java.lang.ClassNotFoundException: org.springframework.core.log.LogDelegateFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more
请求代码
public static void main(String[] args) throws Exception {
//设置请求头
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("appId", "app_service");
HttpEntity request = new HttpEntity(requestHeaders);
RestTemplate restTemplate = new RestTemplate();
RequestCallback requestCallback = restTemplate.httpEntityCallback(request, String.class);
ResponseExtractor<ResponseEntity<String>> responseExtractor = restTemplate.responseEntityExtractor(String.class);
ResponseEntity<String> execute = restTemplate.execute(INTERCEPT_URL + "?orgCode=321300&waybillNo=773011197999686", HttpMethod.GET, requestCallback, responseExtractor, String.class);
System.out.println(execute.getBody());
分析
1.查看RestTemplate.java,发现getForObject和getForEntity都不支持传入请求头参数。
2.getForObject和getForEntity方法最终都是调用execute方法
3.那么我们可以封装好参数,直接调用execute方法