Spring WebClient call to Rest-Service: Exception from Jaxb2XmlDecoder

十年热恋 提交于 2020-02-02 06:30:26

问题


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.


回答1:


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.




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!