我正在尝试向我无法控制的服务器发出 GET 请求,该服务器在 Content-Type
header 中返回无效值:“Application/json; charset= ".
当我尝试执行 GET 调用时,Spring 在尝试解析此值时抛出 InvalidMimeTypeException,因为它正确地期望 = 符号后面应该有一个实际的字符集。
restTemplate.getForObject(new URI(DATASOURCE), String.class); // throws InvalidMimeTypeException when attempting to parse response
是否可以将 RestTemplate 配置为忽略响应中返回的任何内容类型 header ?我无法控制正在查询的服务器。 (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]
最佳答案
你实现了一个自定义 HttpMessageConverter .这里是example .我想您可以重用 JSON 转换器并注册自定义内容类型。
关于java - 强制 Spring RestTemplate 忽略响应的内容类型 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58546298/