如何在应用程序/xml中接受假装响应
Posted
技术标签:
【中文标题】如何在应用程序/xml中接受假装响应【英文标题】:How to accept feign response in application/xml 【发布时间】:2021-03-02 17:00:49 【问题描述】:我正在调用以 XML 格式返回响应的第 3 方 API。
由于我还没有创建任何 POJO 来在我的消费者服务中保持响应,因此我使用 java.lang.Object
进行相同的处理。
我收到以下错误。
org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class java.lang.Object] and content type [application/xml]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:126) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.cloud.openfeign.support.SpringDecoder.decode(SpringDecoder.java:59) ~[spring-cloud-openfeign-core-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.cloud.openfeign.support.ResponseEntityDecoder.decode(ResponseEntityDecoder.java:62) ~[spring-cloud-openfeign-core-2.2.5.RELEASE.jar:2.2.5.RELEASE]
我的假客户端代码
@FeignClient(value = "SERVICE", url = "https://goog.dummyservice/api1/v1", decode404 = true)
public interface UserFeign
@GetMapping(value = "/docs/profile/protocol", consumes = MediaType.APPLICATION_XML_VALUE)
Object getUserData(@RequestHeader("Authorization") String token,
@PathVariable("profile") String profile,
@PathVariable("protocol") String protocol);
我想知道如何通过 feign 保持 XML 响应。
【问题讨论】:
你能解决这个问题吗? 【参考方案1】:FeignClientConfiguration.java
@Configuration
@RequiredArgsConstructor
public class FeignClientConfiguration
private final ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Encoder feignFormEncoder()
return new FormEncoder(new SpringEncoder(messageConverters));
/*@Bean
public Decoder feignDecoder()
MappingJackson2XmlHttpMessageConverter c = new MappingJackson2XmlHttpMessageConverter();
ObjectFactory<HttpMessageConverters> objectFactory = () ->new HttpMessageConverters(c);
return new ResponseEntityDecoder(new SpringDecoder(objectFactory));
*/
ConsultaClient.java
@FeignClient(url = "$api.serpro.url",
name = "name",
configuration = FeignClientConfiguration.class)
public interface ConsultaClient
@PostMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_XML_VALUE)
@Headers("Content-Type: application/x-www-form-urlencoded")
List<DocumentoDTO> getData(ParamsDTO params);
【讨论】:
以上是关于如何在应用程序/xml中接受假装响应的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Spring Boot 中从端点响应中全局省略空 xml 标记?