java – Glassfish:Jersy客户端中的MessageBodyProviderNotFoundException
作者:互联网
嗨所有我试图从头开始创建一个休息网络服务.这是我的服务部分
@Path("/Phones")
public class PhonessResource {
@GET
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public Response getAllNumbers(){
List<PhoneDetail> list = PhoneDirectoryDao.getInstance().getAllNumbers();
GenericEntity<List<PhoneDetail>> entity = new GenericEntity<List<PhoneDetail>>(list) {};
Response response =Response.ok(entity).status(200).build();
return response;//PhoneDirectoryDao.getInstance().getAllNumbers();
}
}
我的数据模型:我有我的getter和setter以及另一个带有所有属性的构造函数,我没有将它粘贴到更少的问题长度,我在客户端和服务器中使用相同的数据模型
@XmlRootElement(name="PhoneDetail")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"id","firstName","lastName","address","phoneNo","timeStamp"})
public class PhoneDetail {
private int id;
private String firstName;
private String lastName;
private String address;
private String phoneNo;
private Timestamp timeStamp;
public PhoneDetail() {}
}
然后我创建一个java客户端来测试服务.我正在使用NETBEANS IDE,所以我在IDE中选择默认选项来创建它
因此我创建了一个Jersey客户端
public class PhoneCLient {
private WebTarget webTarget;
private Client client;
private static final String BASE_URI = "http://localhost:8080/Phones/webresources";
public PhoneCLient() {
client = javax.ws.rs.client.ClientBuilder.newClient();
webTarget = client.target(BASE_URI).path("Items");
}
public <T> T getAllNumbers_XML(Class<T> responseType) throws ClientErrorException {
WebTarget resource = webTarget;
return resource.request(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
}
public <T> T getAllNumbers_JSON(Class<T> responseType) throws ClientErrorException {
WebTarget resource = webTarget;
return resource.request(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
}
}
但它给了我这个错误
Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/xml, type=class org.glassfish.jersey.client.ClientResponse, genericType=class org.glassfish.jersey.client.ClientResponse.
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:173)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:134)
at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:988)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:833)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:768)
at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:96)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:740)
at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:88)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:650)
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:421)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:646)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:375)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:275)
at PhoneDirectoryClient.rest.PhoneCLient.getAllNumbers_XML(PhoneCLient.java:45)
但是当我在浏览器或RestClient Browser插件中测试服务时,它工作正常.
谁能告诉我出了什么问题?
解决方法:
对于Xml,如果你拥有Jersey附带的所有依赖项,它应该可以解决客户端API的问题.您可能没有添加所有这些.我发现你没有使用Maven,我强烈建议你这样做.但我会提供两种方法来处理这个问题.
XML
Maven的:
只有您需要启动并运行客户端的依赖项(支持JAXB xml)
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.13</version>
</dependency>
没有简单得多:-)
非Maven的:
因此,使用Maven项目,我添加了上面的依赖项,这些都是它所引入的传递依赖项.在非Maven项目中,您需要手动添加所有这些jar.
如果你转到Jersey Hompage,请转到下载,并下载“Jersey JAX-RS 2.0 RI软件包.你应该在那里找到所有这些依赖项.你应该将所有需要的东西添加到你的项目中
注意:Netbeans已经附带了Jersey 2.0(JAX-RS RI)库.您只需将该库添加到项目中即可.只需右键单击项目中的[Libraries]节点,然后选择[Add Library].您应该在对话框中看到Jersey.这个解决方案可能是最简单的,但它将导入所有Jersey依赖项,超过客户端API所需的
JSON
JSON需要另一个依赖项:
Maven的:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.13</version>
</dependency>
非Maven的
请查看this post的图像和进一步说明.
只是在类路径上有这些依赖项应该可以工作,没有任何特殊配置.
标签:java,rest,jax-rs,jaxb,jersey-client 来源: https://codeday.me/bug/20190728/1560666.html