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
@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
Thus 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 plugin it works fine . Can anybody tell me what went wrong ??