Glassfish: MessageBodyProviderNotFoundException in Jersy Client

Hi, All I have been trying to create a vacation web service from scratch. Here is my service piece

@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 had my getters and setters along with another constructor that takes the whole property, I didn't insert it to reduce the length of the question, 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 am using NETBEANS IDE, so I select the default option in the IDE to create it

enter image description here

So I create 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 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.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 Browser or RestClient Browser it works fine. Can anyone tell me what went wrong?

+3


source to share


1 answer


For Xml, if you have all the dependencies that come with Jersey, it should develop a field for the client API. You may not have added all of them. I see you are not using Maven, which I would highly recommend. But I will provide both ways to handle this.

XML

Maven:

Only the dependencies you will use to get the client up and running (with JAXB xml support)

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

      

Not much easier :-)

Non-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 need to manually add all these jars.

enter image description here

If you go to Jersey Hompage , go to the Downloads section and download Jersey JAX-RS 2.0 RI bundle

. You should find all these dependencies in there. You should add all the required ones to your project



Note. Netbeans already comes with a library Jersey 2.0 (JAX-RS RI)

. Instead, you can simply add this library to your project. Just right click on the [Libraries] node in your project and select [Add Library]. You should see Jersey in the dialog box. This solution is probably the simplest, but it will import all Jersey dependencies, more than the client API requires

Json

JSON requires a different dependency:

Maven:

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

      

Non-Maven

Check out this post for an image and further explanation.


Simply having these classpath dependencies should work without any special configuration.

+6


source







All Articles