zoukankan      html  css  js  c++  java
  • 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;
        }
  • 相关阅读:
    55. Jump Game
    367. Valid Perfect Square
    22. Generate Parentheses
    254. Factor Combinations
    77. Combinations
    17. Letter Combinations of a Phone Number
    javascript获取随机数的几种方式
    javascript获取随机rgb颜色和十六进制颜色的方法
    javascript遍历数组最优写法
    javascript中字符串的常用方法
  • 原文地址:https://www.cnblogs.com/gqymy/p/13362579.html
Copyright © 2011-2022 走看看