文章目录
一,序
现如今的 IT 项目,由服务端向外发起网络请求的场景,基本上处处可见!
传统情况下,在服务端代码里访问 http 服务时,一般会使用 JDK 的 HttpURLConnection 或者 Apache 的 HttpClient,不过这种方法使用起来太过繁琐,而且 api 使用起来非常的复杂,还得操心资源回收。
其实Spring已经为我们提供了一种简单便捷的模板类来进行操作,它就是RestTemplate
RestTemplate是一个执行HTTP请求的同步阻塞式工具类,它仅仅只是在 HTTP 客户端库(例如 JDK HttpURLConnection,Apache HttpComponents,okHttp 等)基础上,封装了更加简单易用的模板方法 API,方便程序员利用已提供的模板方法发起网络请求和处理,能很大程度上提升我们的开发效率
二,模板注册
Spring RestTemplate 调用REST API 给我们的开发工作带来了极大的方便, 默认的SimpleClientHttpRequestFactory 并不支持https的调用,我们可以通过引入Apache HttpClient实现对https的调用支持。
RestTemplateConfig.java
import java.nio.charset.StandardCharsets;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
/**
*
* 注册 RestTemplate
*
* @author 00fly
* @version [版本号, 2018年11月20日]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
@Configuration
public class RestTemplateConfig
{
@Bean
public RestTemplate restTemplate()
{
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient());
RestTemplate restTemplate = new RestTemplate(requestFactory);
// RestTemplate设置编码
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
return restTemplate;
}
/**
* Apache HttpClient
*
* @return
* @see [类、类#方法、类#成员]
*/
private HttpClient httpClient()
{
// 支持HTTP、HTTPS
Registry<ConnectionSocketFactory> registry =
RegistryBuilder.<ConnectionSocketFactory> create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", SSLConnectionSocketFactory.getSocketFactory()).build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
connectionManager.setMaxTotal(200);
connectionManager.setDefaultMaxPerRoute(100);
connectionManager.setValidateAfterInactivity(2000);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(65000) // 服务器返回数据(response)的时间,超时抛出read timeout
.setConnectTimeout(5000) // 连接上服务器(握手成功)的时间,超时抛出connect timeout
.setConnectionRequestTimeout(1000)// 从连接池中获取连接的超时时间,超时抛出ConnectionPoolTimeoutException
.build();
return HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setConnectionManager(connectionManager).build();
}
}
三,调用打开方式
1,组件引入
除了springboot框架外,主要引入如下组件:
pom.xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
2,调用方式
1)get
@Test
public void testGET1()
{
String url = "https://www.baidu.com/";
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
log.info("ResponseEntity StatusCode={}", responseEntity.getStatusCode());
log.info("ResponseEntity={}", responseEntity);
}
@Test
public void testGET2()
{
String url = "https://www.so.com/";
String responseBody = restTemplate.getForObject(url, String.class);
log.info("responseBody={}", responseBody);
}
2)post
@Test
public void testPOST()
{
// POST请求只能用MultiValueMap
String url = "http://httpbin.org/post";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("id", "123456");
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestEntity, String.class);
log.info("ResponseEntity={}", responseEntity);
}
3)WebService
@Test
public void testWebService()
throws IOException
{
String url = "http://127.0.0.1:7879/demo/services/ws_inputService";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_XML);
headers.setAccept(Arrays.asList(MediaType.TEXT_XML));
InputStream is = this.getClass().getResourceAsStream("/request");
String requestString = IOUtils.toString(is, "utf-8");
HttpEntity<String> requestEntity = new HttpEntity<>(requestString, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestEntity, String.class);
log.info("ResponseEntity Body = {}", responseEntity.getBody());
}
4)JsonRequestBody
/**
* 演示@RequestBody请求
*/
@Test
public void testJsonRequestBody()
{
String url = "https://www.so.com/s";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
// 方式1
SearchReq req = new SearchReq();
req.setPageNo(1);
req.setPageSize(5);
HttpEntity<SearchReq> requestEntity = new HttpEntity<>(req, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestEntity, String.class);
log.info("ResponseEntity={}", responseEntity);
// 方式2
Map<String, String> map = new HashMap<>();
map.put("pageNo", "1");
map.put("pageSize", "10");
HttpEntity<Map<String, String>> requestEntity2 = new HttpEntity<>(map, headers);
ResponseEntity<String> responseEntity2 = restTemplate.postForEntity(url, requestEntity2, String.class);
log.info("ResponseEntity={}", responseEntity2);
// 方式3
map.clear();
map.put("pageNo", "1");
map.put("pageSize", "2");
HttpEntity<String> requestEntity3 = new HttpEntity<>(JSONObject.toJSONString(map), headers);
ResponseEntity<String> responseEntity3 = restTemplate.postForEntity(url, requestEntity3, String.class);
log.info("ResponseEntity={}", responseEntity3);
}
5)get、post 统一调用 restTemplate.exchange
@Test
public void testExchange()
{
String url = "https://www.so.com/s";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.add("token", "Bearer");
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("type", 1);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
ResponseEntity<String> responseEntity;
// GET with headers
responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
log.info("ResponseEntity={}", responseEntity);
// POST
responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
log.info("ResponseEntity={}", responseEntity);
}
6)文件上传 MultipartFile、MultipartFile[ ]
@Test
public void testPOST()
{
// POST请求只能用MultiValueMap
String url = "http://127.0.0.1:8080/post";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
// 注意:FileSystemResource只适用于文件,Jar內运行报错
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
String path = new ClassPathResource("123.jpg").getPath();
FileSystemResource file = new FileSystemResource(path);
// 多次调用,后台接受到的是MultipartFile[]
params.add("file", file);
params.add("file", file);
params.add("file", file);
params.add("name", "girl");
params.add("age", "18");
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestEntity, String.class);
log.info("ResponseEntity={}", responseEntity);
}
@Test
public void testPOST2()
{
// POST请求只能用MultiValueMap
String url = "http://127.0.0.1:8080/post";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
// Resource适用于文件、Jar內
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
Resource file = new ClassPathResource("123.jpg");
// 多次调用,后台接受到的是MultipartFile[]
params.add("file", file);
params.add("file", file);
params.add("file", file);
params.add("name", "girl");
params.add("age", "18");
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestEntity, String.class);
log.info("ResponseEntity={}", responseEntity);
}
7) url支持占位符
@Test
public void testGET()
{
/**** url支持占位符 ****/
String url = "http://localhost:8080/rest/user/list/{page}";
String response = restTemplate.getForObject(url, String.class, 1);
log.info("ResponseBody={}", response);
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class, 1);
log.info("ResponseEntity={}", responseEntity);
}
@Test
public void testPOST()
{
/**** url支持占位符 ****/
String url = "http://localhost:8080/rest/user/list/{page}";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("username", "user001");
params.add("password", "123456");
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
ResponseEntity<String> responseEntity;
String response = restTemplate.postForObject(url, requestEntity, String.class, 1);
log.info("ResponseBody={}", response);
responseEntity = restTemplate.postForEntity(url, requestEntity, String.class, 1);
log.info("ResponseEntity={}", responseEntity);
}
/**
* 演示@RequestBody请求
*/
@Test
public void testJsonRequestBody()
{
String url = "http://127.0.0.1/api/common/search";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
// 方式1
SearchReq req = new SearchReq();
req.setPageNo(1);
req.setPageSize(2);
HttpEntity<SearchReq> requestEntity = new HttpEntity<>(req, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestEntity, String.class);
log.info("ResponseEntity={}", responseEntity);
// 方式2
Map<String, String> map = new HashMap<>();
map.put("pageNo", "1");
map.put("pageSize", "10");
HttpEntity<Map<String, String>> requestEntity2 = new HttpEntity<>(map, headers);
ResponseEntity<String> responseEntity2 = restTemplate.postForEntity(url, requestEntity2, String.class);
log.info("ResponseEntity={}", responseEntity2);
// 方式3
map.clear();
map.put("pageNo", "1");
map.put("pageSize", "2");
HttpEntity<String> requestEntity3 = new HttpEntity<>(JSON.toJSONString(map), headers);
ResponseEntity<String> responseEntity3 = restTemplate.postForEntity(url, requestEntity3, String.class);
log.info("ResponseEntity={}", responseEntity3);
}
@Test
public void testExchange()
{
/**** url支持占位符 ****/
String url = "http://localhost:8080/rest/user/list/{page}";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("username", "用户1");
params.add("password", "123456");
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
ResponseEntity<String> responseEntity;
// GET、POST
responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class, 1);
log.info("ResponseEntity={}", responseEntity);
Map<String, Integer> vars = Collections.singletonMap("page", 1);
responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class, vars);
log.info("ResponseEntity={}", responseEntity);
}
@Data
class SearchReq
{
Integer pageNo = 1;
Integer pageSize = 10;
String keyword;
}
8) 后台下载文件保存
String downUrl = "http://127.0.0.1:8080/file/down";
ResponseEntity<byte[]> responseEntity = restTemplate.getForEntity(downUrl, byte[].class);
byte[] body = responseEntity.getBody();
// 数据落地
File rootDir = new File("upload");
rootDir.mkdirs();
File dest = new File(rootDir.getCanonicalPath() + File.separator + RandomStringUtils.randomNumeric(5) + resource.getFilename());
try (FileOutputStream fos = new FileOutputStream(dest))
{
fos.write(body);
}
9)文件流后台转发
String downUrl = "http://127.0.0.1:8080/file/down";
byte[] bytes = restTemplate.getForEntity(downUrl, byte[].class).getBody();
// 字节流转发到上传接口
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
Resource byteResource = new ByteArrayResource(bytes)
{
@Override
public String getFilename()
{
return resource.getFilename();
}
@Override
public long contentLength()
{
return bytes.length;
}
};
params.add("file", byteResource);
params.add("id", "1");
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
String uploadUrl = "http://127.0.0.1:8080/file/upload";
ResponseEntity<String> responseEntity = restTemplate.postForEntity(uploadUrl, requestEntity, String.class);
log.info("ResponseEntity={}", responseEntity);
四,源码传送
https://gitee.com/00fly/effict-side/tree/master/springboot-http
有任何问题和建议,都可以向我提问讨论,大家一起进步,谢谢!
–over–