大家好我正试图从头开始创建一个休息网络服务。这是我的服务部分
@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中选择默认选项来创建它
因此我创建了一个泽西客户端
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插件中测试服务时,它工作正常。 谁能告诉我出了什么问题?
答案 0 :(得分:7)
对于Xml,如果您拥有Jersey附带的所有依赖项,那么它应该用于客户端API的框。您可能没有添加所有这些。我发现你并没有使用Maven,我强烈建议你这样做。但我会提供两种方法来解决这个问题。
<强>的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,请转到下载,然后下载&#34; Jersey JAX-RS 2.0 RI bundle
。你应该在那里找到所有这些依赖项。您应该将所需的全部内容添加到项目中
注意: Netbeans已经附带Jersey 2.0 (JAX-RS RI)
库。您只需将该库添加到项目中即可。只需右键单击项目中的[Libraries]节点,然后选择[Add Library]。您应该在对话框中看到Jersey。这个解决方案可能是最简单的,但它将导入所有Jersey依赖项,超过客户端API所需的
JSON需要另一个依赖:
<强>的Maven:强>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.13</version>
</dependency>
<强>非Maven的强>
查看this post图片并进一步说明。
只需在类路径上使用这些依赖项就可以了,无需任何特殊配置。