我是REST服务的新手,但是我发现的所有解决方案都是由于缺少必要的提供程序。
依赖项:
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.+"
数据类:
@JacksonXmlRootElement(localName = "Response")
data class ResponseSessionInit(@JacksonXmlProperty(localName = "Error") val error: Error)
data class Error(@JacksonXmlProperty(localName = "Text", isAttribute = true) val text: String)
代理:
interface L3Client {
@POST
@Consumes("text/xml;charset=UTF-8")
@Produces("text/xml; charset=UTF-8")
fun test(string: String): ResponseSessionInit
}
代理方法调用:
val proxy = client.target(API).proxyBuilder(L3Client::class.java).build()
println(proxy.test("<Request><ProtocolInfoGet/></Request>"))
我收到例外:
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)
Resteasy客户端确实找不到XML的解码器,但我确实在路径中。
我可以更改test()方法以返回String并使用类似的东西成功解析XML
val mapper = XmlMapper()
mapper.registerModule(KotlinModule())
val value = mapper.readValue(xml, ResponseSessionInit::class.java)
但是,我想自动完成。我想念的是什么?
答案 0 :(得分:0)
Reasteasy无法使用jackson-xml开箱即用地处理application / xml内容类型。因此,想法是自己编写。
@Provider
@Consumes(MediaType.TEXT_XML, MediaType.APPLICATION_XML)
class BodyReaderProvider : MessageBodyReader<Any> {
//jackson instantiation and some check code here
}
并注册
val client = ResteasyClientBuilder.newBuilder()
.register(BodyReaderProvider())