RestTemplate的设置及使用

时间:2024-05-12 10:05:01

概述

RestTemplate是spring内置的http请求封装,在使用spring的情况下,http请求直接使用RestTemplate是不错的选择。

Rest服务端

使用RestTemplate发起http请求的时候,Rest服务提供者没有什么特殊要求,直接按照传统的SpringMVC的Controller层实现方式实现即可。

举例:

@RestController
@RequestMapping("/user")
public class UserController { private Logger logger = LoggerFactory.getLogger(UserController.class); @RequestMapping("/add")
public UserBean add(UserBean userBean) {
logger.info("request param:{}", JSON.toJSON(userBean));
return userBean;
} }

Rest客户端

使用RestTemplate前先得做一些初始化处理,比如指定http客户端工厂类、设置超时时间、响应参数转换器等。

初始化RestTemplate

@Configuration
@ConditionalOnClass(value = {RestTemplate.class, HttpClient.class})
public class RestTemplateConfiguration { @Value("${remote.maxTotalConnect:0}")
private int maxTotalConnect; //连接池的最大连接数默认为0
@Value("${remote.maxConnectPerRoute:200}")
private int maxConnectPerRoute; //单个主机的最大连接数
@Value("${remote.connectTimeout:2000}")
private int connectTimeout; //连接超时默认2s
@Value("${remote.readTimeout:30000}")
private int readTimeout; //读取超时默认30s //创建HTTP客户端工厂
private ClientHttpRequestFactory createFactory() {
if (this.maxTotalConnect <= 0) {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(this.connectTimeout);
factory.setReadTimeout(this.readTimeout);
return factory;
}
HttpClient httpClient = HttpClientBuilder.create().setMaxConnTotal(this.maxTotalConnect)
.setMaxConnPerRoute(this.maxConnectPerRoute).build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(
httpClient);
factory.setConnectTimeout(this.connectTimeout);
factory.setReadTimeout(this.readTimeout);
return factory;
} //初始化RestTemplate,并加入spring的Bean工厂,由spring统一管理
@Bean
@ConditionalOnMissingBean(RestTemplate.class)
public RestTemplate getRestTemplate() {
RestTemplate restTemplate = new RestTemplate(this.createFactory());
List<HttpMessageConverter<?>> converterList = restTemplate.getMessageConverters(); //重新设置StringHttpMessageConverter字符集为UTF-8,解决中文乱码问题
HttpMessageConverter<?> converterTarget = null;
for (HttpMessageConverter<?> item : converterList) {
if (StringHttpMessageConverter.class == item.getClass()) {
converterTarget = item;
break;
}
}
if (null != converterTarget) {
converterList.remove(converterTarget);
}
converterList.add(1, new StringHttpMessageConverter(StandardCharsets.UTF_8)); //加入FastJson转换器
converterList.add(new FastJsonHttpMessageConverter4());
return restTemplate;
} }

StringHttpMessageConverter默认使用的字符集是ISO-8859-1,在遇到中文的时候会有乱码,所以需要移除RestTemplate默认的StringHttpMessageConverter修改字符字符集后重新设置。

spring的json转换器默认使用的是Jackson,json字符串和对应的Entity如果有字段对不上就会报错,这个有点不符合国情,而FastJson则不会报错,所以很多时候都会用FastJSON替换默认的Jackson。

FastJSON替换Jackson配置

@Configuration
public class FastjsonConfiguration { @Bean
public HttpMessageConverters fastjsonConverter() {
FastJsonConfig fastJsonConfig = new FastJsonConfig();
//自定义格式化输出
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,
SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero); FastJsonHttpMessageConverter4 fastjson = new FastJsonHttpMessageConverter4();
fastjson.setFastJsonConfig(fastJsonConfig);
return new HttpMessageConverters(fastjson);
} }

RestTemplate使用举例

MultiValueMap传参

  @Test
public void multiValueMapParam() {
MultiValueMap<String, String> requestParam = new LinkedMultiValueMap<>();
requestParam.set("name", "张三");
UserBean result = restTemplate
.postForObject("http://127.0.0.1:8280/user/add", requestParam, UserBean.class);
logger.info("result:{}", JSON.toJSON(result));
}

Entity传参

  @Test
public void restClient() {
UserBean userBean = new UserBean();
userBean.setName("王五");
String result = restTemplate
.postForObject("http://127.0.0.1:8280/user/add", userBean, String.class);
logger.info("result:{}", result);
}

以Entity传参,接收端Controller以Entity接收参数的时候需加上@RequestBody注解,否则接收不到参数。

@RequestMapping("/add")
public UserBean add(@RequestBody UserBean userBean) {
logger.info("request param:{}", JSON.toJSON(userBean));
return userBean;
}

原文地址:http://www.itclj.com/blog/5925894681c06e672f942ad6

demo地址:https://github.com/clj198606061111/spring-boot-rest-template-demo