标签:转换 pos private template read set factor ret cto
springboot 使用restTemplate发送post请求,传json数据,结果报错401 Unauthorized: [no body]
添加相应的数据格式就解决了
@Bean public RestTemplate registerTemplate() { RestTemplate restTemplate = new RestTemplate(getFactory()); //这个地方需要配置消息转换器,不然收到消息后转换会出现异常 restTemplate.setMessageConverters(getConverts()); return restTemplate; } private SimpleClientHttpRequestFactory getFactory() { SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setConnectTimeout(connectionTimeout); factory.setReadTimeout(readTimeout); return factory; } private List<HttpMessageConverter<?>> getConverts() { List<HttpMessageConverter<?>> messageConverters = new ArrayList<>(); // String转换器 StringHttpMessageConverter stringConvert = new StringHttpMessageConverter(); List<MediaType> stringMediaTypes = new ArrayList<MediaType>() {{ //添加响应数据格式,不匹配会报401 add(MediaType.TEXT_PLAIN); add(MediaType.TEXT_HTML); add(MediaType.APPLICATION_JSON); }}; stringConvert.setSupportedMediaTypes(stringMediaTypes); messageConverters.add(stringConvert); return messageConverters; }
restTemplate 401 Unauthorized: [no body]
标签:转换 pos private template read set factor ret cto
原文地址:https://www.cnblogs.com/gqymy/p/13362579.html