• restTemplate 401 Unauthorized: [no body]


    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;
        }
  • 相关阅读:
    使用CTE分页 在MSSQL2005上可以使用
    uc_client目录
    用SQL语句添加删除修改字段
    for all your mad scientific needs think geek
    C++:Prototype模式去掉Clone方法
    linux命令:top
    linux命令:time
    C++:运行期断言和编译期断言
    内核分析:EXPORT_SYMBOL解析
    Linux工具:使用SED编辑器
  • 原文地址:https://www.cnblogs.com/gqymy/p/13362579.html
Copyright © 2020-2023  润新知