一、restTemplate默认构造方法
1、getForEntity
发送一个HTTP GET请求,返回ResponseEntity<T>,ResponseEntity<T>是Spring对HTTP请求响应的封装,T代表body要转换的类型
第一个参数是url,第二个参数是返回body的类型,后面是传参
例如:ResponseEntity<UserEntity> responseEntity = restTemplate.getForEntity("http://localhost/get/{id}", UserEntity.class, id);
2、postForEntity
发送一个HTTP POST请求,返回ResponseEntity<T>
第一个参数是url,第二个参数是HttpEntity,第三个参数是返回body的类型
3、getForObject
getForObject实际上是对getForEntity函数的进一步封装,如果你只关注返回的消息体的内容,对其他信息都不关注,此时可以使用getForObject
4、postForObject
同上
二、restTemplate带参数的构造方法
默认的构造方法有个缺点,不能设置超时时间,所以还提供了一个带参的构造方法
public RestTemplate(ClientHttpRequestFactory requestFactory)
基于指定的ClientHttpRequestFactory创建一个实例,不带参数的默认为SimpleClientHttpRequestFactory
1、SimpleClientHttpRequestFactory基于标准JDK创建http连接
2、HttpComponentsClientHttpRequestFactory基于Apache HttpClient创建连接,需要引入apache的包
3、但是没找到处理https请求的方法
三、代码修改
1、MybootConfig.java添加
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
// 避免中文乱码,将字符集从ISO-8859-1改为UTF_8
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
return restTemplate;
}
2、添加测试类RestTemplateTest.java
package myboot;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
import com.example.myboot.MybootApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MybootApplication.class)
public class RestTemplateTest {
@Autowired
private RestTemplate restTemplate;
@Test
public void TestGetForEntity() {
// 提交请求
ResponseEntity<String> response = restTemplate.getForEntity("https://www.baidu.com", String.class);
String result = response.getBody();
HttpHeaders headers = response.getHeaders();
HttpStatus httpStatus = response.getStatusCode();
System.out.println("Get status: " + httpStatus.toString());
System.out.println("Get headers: " + headers.toString());
System.out.println("Get result: " + result);
}
@Test
public void TestPostForEntity() {
// 构建httpHeader
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
// 构建HttpEntity
String request = "{\"aaa\":\"111\"}";
HttpEntity<String> httpEntity = new HttpEntity<String>(request, httpHeaders);
// 提交请求
ResponseEntity<String> response = restTemplate
.postForEntity("http://192.168.23.46:8003/afms-external/action/channelCert", httpEntity, String.class);
String result = response.getBody();
System.out.println("Post result: " + result);
}
@Test
public void TestGetForObject() {
String response = restTemplate.getForObject("https://www.baidu.com", String.class);
System.out.println("Get result: " + response);
}
@Test
public void TestPostForObject() {
String request = "{\"aaa\":\"111\"}";
// 提交请求
String response = restTemplate.postForObject("http://192.168.23.46:8003/afms-external/action/channelCert",
request, String.class);
System.out.println("Post result: " + response);
}
@Test
public void TestPostForObject2() {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(30 * 1000);
requestFactory.setReadTimeout(30 * 1000);
RestTemplate restTemplate2 = new RestTemplate(requestFactory);
String request = "{\"aaa\":\"111\"}";
// 提交请求
String response = restTemplate2.postForObject("http://192.168.23.46:8003/afms-external/action/channelCert",
request, String.class);
System.out.println("Post result2: " + response);
}
}
参考资料:
https://blog.csdn.net/justry_deng/article/details/82531306
https://blog.csdn.net/f641385712/article/details/100713622
https://segmentfault.com/a/1190000018234763
注:最新代码上传至https://github.com/csj50/myboot