I am new at RESTful webservices and was trying to update my @OneToMany
relationship from a standalone client application, but I am not able to do that. I am using the Jersey implementation of JAX-RS that ships with Glassfish 3.1.1.
I have a class A
that has a @OneToMany
relationship with class B
.
MyRestClient
is my standalone client that is calling my RESTful webservice which has been deployed on Glassfish 3.1.1.
MyRestClient.java
public class MyRestClient {
public static void main(String[] args) {
Client client = Client.create();
WebResource resource = client.resource("http://localhost:8080/myapp/rest/a/update/123");
B b1 = new B("debris");
ClientResponse response = resource.put(ClientResponse.class, b1);
System.out.println(response.getEntity(A.class).getTitle() + " has " + response.getEntity(A.class).getBList().size() + " Bs.");
}
}
AResource
is an EJB session bean which I am using as RESTful webservice.
AResource.java
@Stateless
@Path("/a")
public class AResource {
@EJB
private AManager aManager;
@PUT
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
@Path("/update/{id}")
public Response updateA(B b, @PathParam("id") int id) {
A a = aManager.findAById(id);
a.addB(b);
return Response.status(Status.OK).entity(a).build();
}
}
When I run the client I get the following error message: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class myPackage.B, and MIME media type, application/octet-stream, was not found.
Following are the domain objects in my standalone client application which is making a call to the AResource
EJB session bean which I am using as the RESTful webservice.
A.java
@XmlRootElement
public class A implements Serializable{
private List<B> bList = new ArrayList<B>();
public List<B> getBList() {
return bList;
}
//remaining code
}
B.java
public class B implements Serializable {
private String text;
private A a;
@XmlTransient
public A getA() {
return a;
}
public void afterUnmarshal(Unmarshaller u, Object parent) {
this.a = (A) parent;
}
//remaining code
}
Could someone help me understand why this is happening and how I should solve this problem?
You have to do two things to remove this error.
@xmlElement
mapping in the modelThe client side:
response = resource.type(MediaType.APPLICATION_XML).put(ClientResponse.class, b1); //consume
or
response = resource.accept(MediaType.APPLICATION_XML).put(ClientResponse.class, b1); //produce
Make sure that all these libs are in your class path:
Add "Pojo Mapping" and "Jackson Provider" to the jersey client config:
This solve to me!
You need to specify the
@Provider
that@Produces(MediaType.APPLICATION_XML)
from B.classAn add the package of your
MessageBodyWriter<B.class>
to your/WEB_INF/web.xml
as:This also happens if you're missing an empty public constructor for the Entity (could be for JSON, XML etc)..
Include this dependencies in your POM.xml and run Maven -> Update
This can also happen if you've recently upgraded Ant. I was using Ant 1.8.4 on a project, and upgraded Ant to 1.9.4, and started to get this error when building a fat jar using Ant.
The solution for me was to downgrade back to Ant 1.8.4 for the command line and Eclipse using the process detailed here