【问题标题】:How to change response http header in get request by spring RestTemplate?如何通过spring RestTemplate更改获取请求中的响应http标头?
【发布时间】:2017-03-04 05:22:23
【问题描述】:

我有简单的 java spring 方法来创建对象

RestTemplate restTemplate = new RestTemplate();
Address address = restTemplate.getForObject(url, Address.class);

但是服务器用错误的 Content-Type: text/plain 而不是 application/json 响应我的 JSON 字符串(在 Postman 中检查) .我得到了例外:

无法提取响应:找不到适合响应类型 [class Address] 和内容类型 [text/plain;charset=utf-8] 的 HttpMessageConverter

所以我认为,我需要将响应头 Content-Type 更改为正确的 application/json,以便 MappingJackson2HttpMessageConverter 找出 JSON 字符串并运行代码。

【问题讨论】:

    标签: java json spring


    【解决方案1】:

    尝试了一个小时后,我找到了一个简单易行的方法。

    Json 转换器默认只支持“application/json”。我们只是覆盖它以支持“text/plain”。

    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    
    // support "text/plain"
    converter.setSupportedMediaTypes(Arrays.asList(TEXT_PLAIN, APPLICATION_JSON));
    
    RestTemplate template = new RestTemplate();
    template.getMessageConverters().add(converter);
    
    // It's ok now
    MyResult result = tmp.postForObject("http://url:8080/api", 
                new MyRequest("param value"), MyResult.class);
    

    【讨论】:

    • 这就是dump第三方集成的处理方法!
    • 不确定,但在我的情况下,它在发送请求时也会覆盖 MediaType。所以客户端返回 MediaType not supported 错误
    【解决方案2】:

    感谢您的帮助! 以防我无法更改响应的标题。我用正确的标题创建了新的响应对象。

                ClientHttpRequest clientHttpRequest = new SimpleClientHttpRequestFactory().createRequest(URI.create(str), org.springframework.http.HttpMethod.GET);
                final ClientHttpResponse clientHttpResponse = clientHttpRequest.execute();
                MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
                Address address = new Address();
                //It always true, because service always returns 200 OK
                if (clientHttpResponse.getStatusCode() == HttpStatus.OK) {
                    address = (Address) converter.read(address.getClass(), new HttpInputMessage() {
                        public InputStream getBody() throws IOException {
                            return clientHttpResponse.getBody();
                        }
    
                        public HttpHeaders getHeaders() {
                            HttpHeaders httpHeaders = new HttpHeaders();
                            httpHeaders.putAll(clientHttpResponse.getHeaders());
                            httpHeaders.put("Content-Type", Collections.singletonList(MediaType.APPLICATION_JSON_VALUE));
                            return httpHeaders;
                        }
                    });
                    busStop.setNearestAddress(address.toString());
                }
    

    我确信这不是简单而好的解决方案,但它确实有效。

    【讨论】:

      【解决方案3】:

      要为您的请求设置内容类型,您可以执行以下操作:

          HttpHeaders headers = new HttpHeaders();
          headers.setContentType(MediaType.APPLICATION_JSON);
          HttpEntity entity = new HttpEntity(headers);
          ResponseEntity<Address> response = restTemplate.exchange(url, HttpMethod.GET,  entity, Address.class);
          Address address = response.getBody();
      

      【讨论】:

      • 不,它不起作用。它只是更改请求的标头,而不是响应。
      • 您无法更改响应的标头,这些标头是由服务器发送的,因此,除非您正在编写服务器的代码,否则无法做您想做的事情。
      • 检查这个类似的问题stackoverflow.com/questions/24723394/…
      猜你喜欢
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-25
      • 2018-08-08
      • 2021-07-08
      相关资源
      最近更新 更多