2014-01-10 96 views
5

我決定搬到Jersey 2.5,我在Java Beans轉換爲JSON時遇到了一些問題。我有一個User Bean,並且我試圖返回一個服務列表users從Jersey 1. *升級到Jersey 2.5

其獲取用戶的列表我的服務代碼:

@GET 
@Path("getAllUsers") 
@Produces(MediaType.APPLICATION_JSON) 
public List<User> getAllUsers() { 
    //Fetching users list here 
    return users; 
} 

我獲得以下服務器控制檯的例外:

[org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=java.util.List<com.example.User>.] with root cause 
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=java.util.List<com.example.User>. 
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:227) 
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:149) 
    at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:103) 
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:149) 
    at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:88) 
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:149) 
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1139) 
    at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:574) 
    at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:381) 
    at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:371) 
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:262) 

我缺少的任何相關性或任何特定的2.5配置?

+0

沒有。你使用哪個JSON提供者?你在GF4上運行還是在使用其他容器? –

+0

@MichalGajdos Jackson和Tomcat。 –

+0

謝謝,請說清楚。您依賴於'jersey-media-json-jackson'或直接在Jackson 2.x上,而不是其他JSON提供者(即'jersey-media-moxy')。我對嗎? –

回答

5

由於您使用的傑克遜1.9.x的一個(不)馬歇爾JSON,請確保您有jersey-media-json-jackson模塊中你所依賴的列表和你登記在你的應用JacksonFeature,即:

public class MyResourceConfig extends ResourceConfig { 

    public MyResourceConfig() { 
     // ... other registrations ... 

     register(JacksonFeature.class); 
    } 
} 

要查看更多如何在澤西島2註冊供應商的方法,請參閱此article

+0

非常感謝。它工作完美。 –