【问题标题】:Force Spring RestTemplate to ignore response's content type headers强制 Spring RestTemplate 忽略响应的内容类型标头
【发布时间】:2019-10-24 17:19:40
【问题描述】:

我正在尝试向我无法控制的服务器发出 GET 请求,该请求在 Content-Type 标头中返回无效值:“Application/json; charset="。

当我尝试进行 GET 调用时,Spring 会在尝试解析此值时抛出 InvalidMimeTypeException,因为它正确地期望在 = 符号之后应该有一个实际的字符集。

restTemplate.getForObject(new URI(DATASOURCE), String.class); // throws InvalidMimeTypeException when attempting to parse response

是否可以将 RestTemplate 配置为忽略响应中返回的任何内容类型标头?我无法控制我正在查询的服务器。 (Spring-Boot 2.2.0.RELEASE。)

org.springframework.http.InvalidMediaTypeException: Invalid mime type "Application/json; charset=": 'value' must not be empty
    at org.springframework.http.MediaType.parseMediaType(MediaType.java:574) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:966) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.web.client.HttpMessageConverterExtractor.getContentType(HttpMessageConverterExtractor.java:135) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:92) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:744) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:717) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:336) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]

【问题讨论】:

    标签: java spring resttemplate


    【解决方案1】:

    您实现了自定义HttpMessageConverter。这里是example。我猜你可以重用 JSON 转换器并注册自定义内容类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 2012-02-06
      • 1970-01-01
      • 2021-05-23
      • 2022-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多