gpt4 book ai didi

java - 自定义类上的 readEntity 抛出 MessageBodyProviderNotFoundException

转载 作者:行者123 更新时间:2023-11-30 02:10:22 25 4
gpt4 key购买 nike

我正在用 Java 为一个类开发 RESTful API。我正在使用 JAX-RS 我遵循了本教程 "JAX-RS Jersey" 。一切正常,除了当我尝试执行 response.readEntity(MyTable.class) 来创建我的 MyTable 类的新 Object 时,一切都按预期进行。已经创造出来了。这对于 String.class 来说效果很好,它将所有 JSON 信息作为字符串读取,但它无法将其解析为新的 Object我的定制类(class)。我已经阅读了很多这方面的内容,并且已经设置了属性的所有 setter 和 getter 以及一个空的构造函数,但它无法将信息解析为我的自定义类的新对象。我收到 MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/json, type=class com.beans.MyTable, genericType=class com.beans.MyTable. 错误。

MyTable.java

 private Integer id;
private String name;

public MyTable() {

}

public MyTable(Integer id, String name) {
this.id = id;
this.name = name;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "MyTable [id=" + id + ", name=" + name + "]";
}

MyClient.java

Client client = ClientBuilder.newClient( new ClientConfig().register( LoggingFeature.class ) );
WebTarget webTarget = client.target("http://localhost:8080/App/rest").path("test");

Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.get();

//Does not work and gives error
MyTable table = response.readEntity(MyTable.class);
System.out.println(table.toString());

MyService.java

@PermitAll
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response test() {
MyTable table = new MyTable();
table.setId(1);
table.setName("mesa de teste");

GenericEntity<MyTable> genericEntity = new GenericEntity<MyTable>(table, MyTable.class);
return Response.ok().entity(genericEntity).build();
}

错误

Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/json, type=class com.beans.MyTable, genericType=class com.beans.MyTable.
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:231)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)
at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1085)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:874)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:808)
at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:326)
at org.glassfish.jersey.client.InboundJaxrsResponse$1.call(InboundJaxrsResponse.java:115)
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.runInScopeIfPossible(InboundJaxrsResponse.java:267)
at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:112)
at com.client.MyClient.main(MyClient.java:31)

最佳答案

根据链接的指南,作者使用 Gson 创建了一个客户 JSON 提供程序。您需要向客户端注册此提供程序,就像您在服务器端所做的那样。

new ClientConfig()
.register( LoggingFeature.class )
.register( GsonMessageBodyHandler.class )

阅读chapter 8 and 9 of the Jersey docs了解有关提供程序以及如何使用它们在 Jersey 中进行序列化和反序列化的更多信息。

关于java - 自定义类上的 readEntity 抛出 MessageBodyProviderNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50295567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com