Java Glassfish:Jersy 客户端中的MessageBodyProviderNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26945281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Glassfish :MessageBodyProviderNotFoundException in Jersy Client
提问by edwin
Hi All I was trying to create a rest web-service from scratch. Here is my Service part
大家好我试图从头开始创建一个休息网络服务。这是我的服务部分
@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();
}
}
My data Model : I a had my getters and setters along with another constructor that take all property ,I didn't paste it to less the question length ,I use the same data model in client and server
我的数据模型:我有我的 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() {}
}
Then I create a java client to test the service .I am using NETBEANS IDE ,so I choose default option in IDE to create it
然后我创建了一个 java 客户端来测试服务。我使用的是 NETBEANS IDE,所以我在 IDE 中选择了默认选项来创建它
Thus I create a Jersey Client
因此我创建了一个 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);
}
}
But It gives me this error
但它给了我这个错误
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.access0(JerseyInvocation.java:88)
at org.glassfish.jersey.client.JerseyInvocation.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)
But when I test the service in Browser or RestClient Browser plugin it works fine . Can anybody tell me what went wrong ??
但是当我在浏览器或 RestClient 浏览器插件中测试服务时,它工作正常。谁能告诉我出了什么问题?
采纳答案by Paul Samsotha
For Xml, if you have all the dependencies that come with Jersey, it should work out the box for the client API. You might not have added all of them. I see you aren't using Maven, which I would strongly advise doing. But I'll provide both way to handle this.
对于 Xml,如果您拥有 Jersey 附带的所有依赖项,它应该为客户端 API 工作。您可能没有添加所有这些。我看到您没有使用 Maven,我强烈建议您这样做。但我会提供两种方法来处理这个问题。
XML
XML
Maven:
马文:
Only dependencies you'll nee to get the client up and running (with JAXB xml support)
只有您需要启动和运行客户端的依赖项(使用 JAXB xml 支持)
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.13</version>
</dependency>
Doesn't get much simpler :-)
并没有变得更简单:-)
Non-Maven:
非Maven:
So using a Maven project, I added the above dependency, and these are all the transitive dependencies it pulled in. In your non Maven project, you will need to manually add all these jars.
所以使用一个Maven项目,我添加了上面的依赖,这些都是它拉进来的传递依赖。在你的非Maven项目中,你需要手动添加所有这些jar。
If you go to the Jersey Hompage, go to Downloads, and download the "Jersey JAX-RS 2.0 RI bundle
. You should find all these dependencies in there. You should add all the ones needed, into your project
如果您转到Jersey Hompage,请转到 Downloads,然后下载 " Jersey JAX-RS 2.0 RI bundle
。您应该在其中找到所有这些依赖项。您应该将所有需要的依赖项添加到您的项目中
Note:Netbeans already comes with the Jersey 2.0 (JAX-RS RI)
library. You could instead simply add that library to your project. Just right click on the [Libraries] node in your project, and select [Add Library]. You should see the Jersey in the dialog. This solution is probably the easiest, but it will import all the Jersey dependencies, more than is required for the client API
注意:Netbeans 已经随Jersey 2.0 (JAX-RS RI)
库一起提供。您可以简单地将该库添加到您的项目中。只需右键单击项目中的 [Libraries] 节点,然后选择 [Add Library]。您应该会在对话框中看到球衣。这个解决方案可能是最简单的,但它会导入所有 Jersey 依赖项,比客户端 API 所需的更多
JSON
JSON
JSON requires another dependency:
JSON 需要另一个依赖项:
Maven:
马文:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.13</version>
</dependency>
Non-Maven
非 Maven
Have a look at this postfor an image and further explanation.
看看这个帖子的图片和进一步的解释。
Just having these dependencies on the classpath should work, without any special configuration.
只需在类路径上拥有这些依赖项即可,无需任何特殊配置。