使用GenericType在JSON Web服务上的MessageBodyProviderNotFoundException

时间:2018-02-12 16:29:49

标签: java json rest web-services

我在Webservice客户端上阅读GenericType时遇到问题。

这是我的网络服务:

    @GET
@Path("/results")
@Produces(MediaType.APPLICATION_JSON)
public Response getPlayerResults(@QueryParam("nick") String nick, @Context HttpServletRequest request,
        @Context HttpServletResponse response) {

    // if (!isAuth(request, response)) {
    // authError(response);
    // }

    System.out.println(nick);
    GenericEntity<List<JSONTournament>> entity = new GenericEntity<List<JSONTournament>>(
            MongoTournaments.getPlayerResults(nick, null)) {
    };

    return Response.ok(entity).build();
}

我的客户:

Client client = ClientBuilder.newClient();

        WebTarget webTarget = client.target("http://localhost:8080/WS").path("players/results");

        Response response = webTarget.queryParam("nick", nick).request(MediaType.APPLICATION_JSON).get();

        System.out.println(nick);   

        tournaments = response.readEntity(new GenericType<List<JSONTournament>>() {
        });

我得到了这个例外和一个奇怪的媒体类型(text / html)而不是application / json ......:

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/html;charset=utf-8, type=interface java.util.List, genericType=java.util.List<com.winascrap.database.model.JSONTournament>.
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:232)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:156)
at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1085)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:853)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:812)
at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:368)
at org.glassfish.jersey.client.InboundJaxrsResponse$2.call(InboundJaxrsResponse.java:122)
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:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:419)
at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:119)

我不明白遗失或未正确完成的事情。 我的对象有@XmlRootElement注释,我使用jersey.core,jersey.media和moxy作为依赖...(但我可能忘记了依赖或配置?):

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

解决了!

我错过了将一个空构造函数添加到使用@XmlRootElement注释的自定义类型。它只是添加没有参数的构造函数。