Java – GlassFish: messagebodyprovidernotfoundexception in Jersey client

Hi, all I tried to create a rest web service from scratch This 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 have my getter and setter and another constructor with all attributes. I don't paste it into less problem length. I use the same data model on the client and server

@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'm using the NetBeans IDE, so I choose the default option in the IDE to create it

So I created a Jersey client

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 gave me this mistake

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)

But when I test the service in the browser or restclient browser plug-in, it works normally Who can tell me what's wrong?

Solution

For XML, if you have all the dependencies attached to Jersey, it should solve the problem of client API You may not have added all of these I found you didn't use maven, and I strongly recommend that you do But I will offer two ways to deal with this problem

XML

Maven's:

Only you need to start and run the client's dependencies (JAXB XML support)

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.13</version>
</dependency>

Not much simpler: -)

Non Maven:

Therefore, using the Maven project, I added the above dependencies, which are the transfer dependencies it introduces In non Maven projects, you need to add all these jars manually

If you go to Jersey Humpage, go to download and download the Jersey jax-rs 2.0 RI package. You should find all these dependencies there. You should add everything you need to your project

Note: NetBeans already comes with Jersey 2.0 (jax-rs RI) library You just need to add the library to the project Just right-click the [libraries] node in the project and select [add library] You should see Jersey. Com in the dialog box This solution may be the simplest, but it will import all Jersey dependencies, more than required by the client API

JSON

JSON requires another dependency:

Maven's:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>2.13</version>
</dependency>

Non maven

Please view the image of this post and further instructions

Just having these dependencies on the classpath should work without any special configuration

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>