RestTemplate初始化配置和FastJSON替换Jackson配置
package org.rabbit.consumer.config;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
/**
* @author <font color="red"><b>Gong.YiYang</b></font>
* @Date 2018年7月12日
* @Version
* @Description
*/
@Configuration
@ConditionalOnClass(value = {RestTemplate.class, HttpClient.class})
public class RestTemplateConfiguration {
@Value("${remote.maxTotalConnect}")
private int maxTotalConnect; //连接池的最大连接数默认为0
@Value("${remote.maxConnectPerRoute}")
private int maxConnectPerRoute; //单个主机的最大连接数
@Value("${remote.connectTimeout}")
private int connectTimeout; //连接超时默认2s
@Value("${remote.readTimeout}")
private int readTimeout; //读取超时默认30s
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;
}
@Bean
@ConditionalOnMissingBean(RestTemplate.class)
public RestTemplate getRestTemplate() {
RestTemplate restTemplate = new RestTemplate(this.createFactory());
List<HttpMessageConverter<?>> converterList = restTemplate.getMessageConverters();
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));
converterList.add(new FastJsonHttpMessageConverter4());
return restTemplate;
}
}
package org.rabbit.consumer.config;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
/**
* @author <font color="red"><b>Gong.YiYang</b></font>
* @Date 2018年7月12日
* @Version
* @Description 把spring-boot默认的json解析器由jackson换为fastjson
*/
@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);
}
}
调用接口实例:封装数据使用JSONObect
JSONObject object = new JSONObject();
object.put("pic_id","水电费");
object.put("pic", img);
List<JSON> mapObject = new ArrayList<>();
mapObject.add(object);
JSONObject object1 = new JSONObject();
object1.put("id","1");
object1.put("data",mapObject);
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<JSONObject> httpEntity = new HttpEntity<>(object1, header);
//JSONObject postForObject = restTemplate.postForObject(url, object1, JSONObject.class);
ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, httpEntity,String.class);
System.out.println(postForEntity.getBody());
还没有评论,来说两句吧...