RestTemplate初始化配置和FastJSON替换Jackson配置

灰太狼 2022-05-17 05:39 310阅读 0赞
  1. package org.rabbit.consumer.config;
  2. import java.nio.charset.StandardCharsets;
  3. import java.util.List;
  4. import org.apache.http.client.HttpClient;
  5. import org.apache.http.impl.client.HttpClientBuilder;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  8. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.http.client.ClientHttpRequestFactory;
  12. import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
  13. import org.springframework.http.client.SimpleClientHttpRequestFactory;
  14. import org.springframework.http.converter.HttpMessageConverter;
  15. import org.springframework.http.converter.StringHttpMessageConverter;
  16. import org.springframework.web.client.RestTemplate;
  17. import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
  18. /**
  19. * @author <font color="red"><b>Gong.YiYang</b></font>
  20. * @Date 2018年7月12日
  21. * @Version
  22. * @Description
  23. */
  24. @Configuration
  25. @ConditionalOnClass(value = {RestTemplate.class, HttpClient.class})
  26. public class RestTemplateConfiguration {
  27. @Value("${remote.maxTotalConnect}")
  28. private int maxTotalConnect; //连接池的最大连接数默认为0
  29. @Value("${remote.maxConnectPerRoute}")
  30. private int maxConnectPerRoute; //单个主机的最大连接数
  31. @Value("${remote.connectTimeout}")
  32. private int connectTimeout; //连接超时默认2s
  33. @Value("${remote.readTimeout}")
  34. private int readTimeout; //读取超时默认30s
  35. private ClientHttpRequestFactory createFactory() {
  36. if (this.maxTotalConnect <= 0) {
  37. SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
  38. factory.setConnectTimeout(this.connectTimeout);
  39. factory.setReadTimeout(this.readTimeout);
  40. return factory;
  41. }
  42. HttpClient httpClient = HttpClientBuilder.create().setMaxConnTotal(this.maxTotalConnect)
  43. .setMaxConnPerRoute(this.maxConnectPerRoute).build();
  44. HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(
  45. httpClient);
  46. factory.setConnectTimeout(this.connectTimeout);
  47. factory.setReadTimeout(this.readTimeout);
  48. return factory;
  49. }
  50. @Bean
  51. @ConditionalOnMissingBean(RestTemplate.class)
  52. public RestTemplate getRestTemplate() {
  53. RestTemplate restTemplate = new RestTemplate(this.createFactory());
  54. List<HttpMessageConverter<?>> converterList = restTemplate.getMessageConverters();
  55. HttpMessageConverter<?> converterTarget = null;
  56. for (HttpMessageConverter<?> item : converterList) {
  57. if (StringHttpMessageConverter.class == item.getClass()) {
  58. converterTarget = item;
  59. break;
  60. }
  61. }
  62. if (null != converterTarget) {
  63. converterList.remove(converterTarget);
  64. }
  65. converterList.add(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
  66. converterList.add(new FastJsonHttpMessageConverter4());
  67. return restTemplate;
  68. }
  69. }
  70. package org.rabbit.consumer.config;
  71. import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
  72. import org.springframework.context.annotation.Bean;
  73. import org.springframework.context.annotation.Configuration;
  74. import com.alibaba.fastjson.serializer.SerializerFeature;
  75. import com.alibaba.fastjson.support.config.FastJsonConfig;
  76. import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
  77. /**
  78. * @author <font color="red"><b>Gong.YiYang</b></font>
  79. * @Date 2018年7月12日
  80. * @Version
  81. * @Description 把spring-boot默认的json解析器由jackson换为fastjson
  82. */
  83. @Configuration
  84. public class FastjsonConfiguration {
  85. @Bean
  86. public HttpMessageConverters fastjsonConverter() {
  87. FastJsonConfig fastJsonConfig = new FastJsonConfig();
  88. //自定义格式化输出
  89. fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,
  90. SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero);
  91. FastJsonHttpMessageConverter4 fastjson = new FastJsonHttpMessageConverter4();
  92. fastjson.setFastJsonConfig(fastJsonConfig);
  93. return new HttpMessageConverters(fastjson);
  94. }
  95. }

调用接口实例:封装数据使用JSONObect

  1. JSONObject object = new JSONObject();
  2. object.put("pic_id","水电费");
  3. object.put("pic", img);
  4. List<JSON> mapObject = new ArrayList<>();
  5. mapObject.add(object);
  6. JSONObject object1 = new JSONObject();
  7. object1.put("id","1");
  8. object1.put("data",mapObject);
  9. HttpHeaders header = new HttpHeaders();
  10. header.setContentType(MediaType.APPLICATION_JSON_UTF8);
  11. HttpEntity<JSONObject> httpEntity = new HttpEntity<>(object1, header);
  12. //JSONObject postForObject = restTemplate.postForObject(url, object1, JSONObject.class);
  13. ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, httpEntity,String.class);
  14. System.out.println(postForEntity.getBody());

发表评论

表情:
评论列表 (有 0 条评论,310人围观)

还没有评论,来说两句吧...

相关阅读