I'm currentlty struggling with Springs reactive WebClient calling a Rest-Service. I get an UnsupportedOperationException of the decodeToMono-function from Springs Jaxb2XmlDecoder.
public Mono<T> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
throw new UnsupportedOperationException();
}
My WebClient call:
ResponseEntity<MyAsyncResponse> result = webClient.post()
.contentType(MediaType.APPLICATION_XML)
.syncBody(payload)
.exchange()
.flatMap(res -> res.toEntity(MyAsyncResponse.class))
.block();
The Rest-Service:
@RestController
public class MyAsyncController {
@PostMapping(path = "foo")
private CompletableFuture<MyAsyncResponse> getFoo() {
return getMyAsyncResponse();
}
...
What do I have to configure, that a suitable Decoder-implementation is used? It's a plain old Spring-MVC (v5.0.3) application, not Spring-Boot.
Jaxb2XmlDecoder did not implement decodeToMono, but it is now fixed via SPR-16759. So just upgrading to Spring Framework 5.0.6+ / Spring Boot 2.0.2+ should avoid the reported exception.
This is known issue of the Jaxb2XmlDecoder in Spring 5.0.3, which is unable to decode simple single message (however, it can decode message streaming).
See base class - https://github.com/spring-projects/spring-framework/blob/master/spring-core/src/main/java/org/springframework/core/codec/AbstractDecoder.java
Jaxb2XmlDecoder does not override "decodeToMono" method.
To fix this create your own decoder and update configuration by the following style:
On Kotlin:
@Bean
open fun webFluxConfigurer(myReader: MyReader,
myWriter: MyWriter): WebFluxConfigurer {
return object : WebFluxConfigurer {
override fun configureHttpMessageCodecs(configurer: ServerCodecConfigurer) {
// disable default codecs, because of problematic XML serialization in Jaxb2XmlDecoder
configurer.registerDefaults(false)
configurer.customCodecs().decoder(myReader)
configurer.customCodecs().encoder(myWriter)
}
}
}
Or on Java:
@Bean
WebFluxConfigurer webFluxConfigurer(MyReader myReader, MyWriter myWriter){
return new WebFluxConfigurer() {
@Override
public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
// disable default codecs, because of problematic XML serialization in Jaxb2XmlDecoder
configurer.registerDefaults(false);
configurer.customCodecs().decoder(myReader);
configurer.customCodecs().encoder(myWriter);
}
};
}
来源:https://stackoverflow.com/questions/48851769/spring-webclient-call-to-rest-service-exception-from-jaxb2xmldecoder