Issue
I'm developing a web service using RESTful web service of Netbeans. The service class contains following method:
@Path("jpost")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public String jpost(Car c){
return "Car:"+c.getTag()+"\t"+c.getName()+"\t"+c.getColor();
}
For testing this method, I'm using Netbeans' RESTful Java Client to generate test method. The result is:
public String jpost(Object requestEntity) throws ClientErrorException {
return webTarget.path("jpost").request(javax.ws.rs.core.MediaType.APPLICATION_JSON).post(javax.ws.rs.client.Entity.entity(requestEntity, javax.ws.rs.core.MediaType.APPLICATION_JSON), String.class);
}
When i call jpost
following error appear:
Exception in thread "main" javax.ws.rs.NotAcceptableException: HTTP 406 Not Acceptable
at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:923)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:770)
at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:90)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:671)
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:422)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:667)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:423)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:326)
at myserver.test5.jpost(test5.java:36)
at myserver.MyServerTest.main(MyServerTest.java:32)
What is the problem?
Solution
Your service only produces text/plain
responses.
HTTP code 406 means that server cannot provide a content type acceptable by the client. So the client either specified a wrong content type in Accepts
header, or did not send Accepts
at all.
To specify the accepted type, request(...)
argument is used.
public String jpost(Object requestEntity) throws ClientErrorException
{
return webTarget.path("jpost")
.request(javax.ws.rs.core.MediaType.TEXT_PLAIN)
.post(javax.ws.rs.client.Entity.entity(requestEntity, javax.ws.rs.core.MediaType.APPLICATION_JSON), String.class);
}
Answered By - Roman Puchkovskiy