未找到Kotlin MessageBodyWriter的JAX-RS

时间:2017-07-24 13:56:00

标签: rest jersey kotlin

我已经用Kotlin和(b)用Java实现了JAX-RS资源(a)。虽然Java风味按预期工作,但Kotlin风味失败了 MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=java.util.List<? extends ....Entry>.|#]例外。

这是我的资源

@Path("demokotlin")
class DemoKotlinResource {
    @GET
    @Path("responseGenericEntityWithArrayList")
    fun arrayList(): Response {
        val list = arrayListOf(Entry("responseGenericEntityWithArrayList"))
        val entity = object : GenericEntity<List<Entry>>(list) {}
        return Response
                .status(Response.Status.OK)
                .entity(entity)
                .type(MediaType.APPLICATION_JSON).build()
    }
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
open class Entry(val fieldName: String)

集成测试基本上是

private val client = ClientBuilder.newClient()
private val target = client.target("http://localhost:8080/xyz/resources/demokotlin")

@Test
fun responseGenericEntityWithArrayList() {
    val response = target.path("responseGenericEntityWithArrayList").request(MediaType.APPLICATION_JSON).get()
    assertThat("no ok status", response.status, equalTo(200))
    val entity = response.readEntity(JsonArray::class.java)
    assertThat(entity.getJsonObject(0).getString("fieldName"), equalTo("responseGenericEntityWithArrayList"))
    assertThat(entity.toString(), equalTo("[{\"fieldName\":\"responseGenericEntityWithArrayList\"}]"))
}

Java中的等效代码工作正常,而Kotlin中显示的代码抛出MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=java.util.List<? extends ....Entry>.|#]

我正在使用Payara 4.1.2.172。通过一些JAX-RS类,我发现Java代码导致了

genericType=java.util.List<....Entry>

(没有? extends)而Kotlin的通用类型信息是

genericType=java.util.List<? extends ....Entry>
那可能是原因吗?如果是这样,我该如何解决这个问题?

编辑2017-07-25 这是堆栈跟踪

#|2017-07-25T12:33:16.932+0000|INFO|Payara 4.1||_ThreadID=32;_ThreadName=http-thread-pool::http-listener-1(1);_TimeMillis=1500985996932;_LevelValue=800;|
javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
        at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:90)
        at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
        at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1130)
        at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:711)
        at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:444)
        at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:434)
        at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:329)
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
        at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
        at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
        at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
        at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
        at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1606)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:338)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.glassfish.tyrus.servlet.TyrusServletFilter.doFilter(TyrusServletFilter.java:305)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:250)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:654)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:593)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238)
        at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:466)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:169)
        at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
        at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
        at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
        at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
        at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
        at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539)
        at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
        at java.lang.Thread.run(Thread.java:748)
Caused by: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=java.util.List<? extends com.github.reproducer.boundary.Entry>.
        at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)
        at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
        at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:106)
        at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
        at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:86)
        ... 52 more
        |#]

编辑#2:工作 Java代码 - 应该与Kotlin变种非常相似

@Path("demojava")
public class DemoJavaResource {

    @GET
    @Path("responseGenericEntityWithArrayList")
    public Response arrayList() {
        final List<JEntry> list = new ArrayList<>();
        list.add(new JEntry("responseGenericEntityWithArrayList"));
        final GenericEntity<List<JEntry>> entity = new GenericEntity<List<JEntry>>(list) {};
        return Response
            .status(Response.Status.OK)
            .entity(entity)
            .type(MediaType.APPLICATION_JSON).build();
    }
}

和JEntry

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class JEntry {
    private String fieldName;

    JEntry() { this(""); }

    JEntry(final String responseGenericEntityWithArrayList) {
        fieldName = responseGenericEntityWithArrayList;
    }

    public String getFieldName() { return fieldName; }
}

1 个答案:

答案 0 :(得分:0)

因为JAX-RS无法将ArrayList编码为JSON数组。

MessageBodyWriter是将Java数据类型转换为输出格式的处理程序,例如 XML,JSON。 JAX-RS内置MessageBodyWriter用于主要数据类型(包括JAXB),但不包括ArrayList。因此,您需要创建一个自定义MessageBodyWriter来接受ArrayList并编码为JSON。

但在这种情况下,不需要任何自定义MessageBodyWriter。而不是,您需要重新创建Entry类。对不起,我不能写Kotlin样本,但是如果它是用Java编写的,那就是以下内容;

@Path("demokotlin")
public class DemoKotlinResource {

    @GET
    @Path("responseGenericEntityWithArrayList")
    public Response arrayList() {
    Entry entity = new Entry();
        entity.fieldName.add("responseGenericEntityWithArrayList");
        return Response
                .status(Response.Status.OK)
                .entity(entity)
                .type(MediaType.APPLICATION_JSON).build();
    }
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Entry {
    public List<String> fieldName = new ArrayList<>();
}

它转化为Kotlin,然后它可能已经完成了。