Search code examples
kotlinclientresteasyjackson2

RESTEASY003145: Unable to find a MessageBodyReader of content-type text/xml;charset=UTF-8


I'm a newbie in a REST services, but all solutions I found was due to lack of necessary providers.

Dependencies:

compile group: 'org.jboss.resteasy', name: 'resteasy-client', version: '3.6.1.Final'
compile group: 'org.jboss.resteasy', name: 'resteasy-jackson2-provider', version: '3.6.1.Final'
compile "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.+"
compile "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.+"

Data class:

@JacksonXmlRootElement(localName = "Response")
data class ResponseSessionInit(@JacksonXmlProperty(localName = "Error") val error: Error)
data class Error(@JacksonXmlProperty(localName = "Text", isAttribute = true) val text: String)

Proxy:

interface L3Client {

  @POST
  @Consumes("text/xml;charset=UTF-8")
  @Produces("text/xml; charset=UTF-8")
  fun test(string: String): ResponseSessionInit
}

Proxy method invocation:

    val proxy = client.target(API).proxyBuilder(L3Client::class.java).build()
    println(proxy.test("<Request><ProtocolInfoGet/></Request>"))

I receive exception:

javax.ws.rs.client.ResponseProcessingException: javax.ws.rs.ProcessingException: RESTEASY003145: Unable to find a MessageBodyReader of content-type text/xml;charset=UTF-8 and type class ru.abinet.commons.cft.L3.messages.ResponseSessionInit

at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:156)
at org.jboss.resteasy.client.jaxrs.internal.proxy.extractors.BodyEntityExtractor.extractEntity(BodyEntityExtractor.java:60)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invokeSync(ClientInvoker.java:150)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:112)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
at com.sun.proxy.$Proxy19.test(Unknown Source)

It really looks like if Resteasy client can't find a decoder for my XML, however I have it in path.

I can change test() method to return String and successfully parse XML with something like

    val mapper = XmlMapper()
    mapper.registerModule(KotlinModule())
    val value = mapper.readValue(xml, ResponseSessionInit::class.java)

However, I'd like to do it automagically. What I'm missing ?


Solution

  • Reasteasy can't handle application/xml content-type out of the box with jackson-xml. So the idea is to write it by yourself.

    @Provider
    @Consumes(MediaType.TEXT_XML, MediaType.APPLICATION_XML)
    class BodyReaderProvider : MessageBodyReader<Any> {
    
       //jackson instantiation and some check code here
    }
    

    and register it

    val client = ResteasyClientBuilder.newBuilder()
                   .register(BodyReaderProvider())