RestTemplate 不支持[text/html;charset=UTF-8]
将自定义的Converter加入restTemplate的messageConverters list中
public class MyMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
public MyMappingJackson2HttpMessageConverter(){
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.TEXT_PLAIN);
mediaTypes.add(MediaType.TEXT_HTML); //加入text/html类型的支持
setSupportedMediaTypes(mediaTypes);
}
}
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
RestTemplate restTemplate = new RestTemplate(factory);
restTemplate.getMessageConverters().add(new MyMappingJackson2HttpMessageConverter());
return restTemplate;
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
// ms
factory.setReadTimeout(5000);
// ms
factory.setConnectTimeout(15000);
return factory;
}
}
自己拼接请求头
public Map<String, Object> requestPostTextPlanUTF_8(String requestAddress,Map<String,Object> conditionMap){
Map<String, Object> reqMap = new HashMap<>();
reqMap.put("sitecode", sitecode);
reqMap.put("sitekey", sitekey);
reqMap.put("condition", conditionMap);
HttpHeaders uploadHeaders = new HttpHeaders();
uploadHeaders.setContentType(new MediaType(MediaType.TEXT_PLAIN, StandardCharsets.UTF_8));
HttpEntity<Map<String, Object>> uploadRequest = new HttpEntity<>(reqMap, uploadHeaders);
//获取库存信息
return restTemplate.postForObject(url + requestAddress, uploadRequest, Map.class);
}