skip skip - 1 year ago 239
reST (reStructuredText) Question

A message body writer for Java type, class myPackage.B, and MIME media type, application/octet-stream, was not found

I am new at RESTful webservices and was trying to update my

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
that has a
relationship with class

is my standalone client that is calling my RESTful webservice which has been deployed on Glassfish 3.1.1.

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.");

is an EJB session bean which I am using as RESTful webservice.

public class AResource {

private AManager aManager;

public Response updateA(B b, @PathParam("id") int id) {
A a = aManager.findAById(id);
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
EJB session bean which I am using as the RESTful webservice.

public class A implements Serializable{

private List<B> bList = new ArrayList<B>();
public List<B> getBList() {
return bList;
//remaining code


public class B implements Serializable {

private String text;
private A a;

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?

Answer Source

In your client code you are not specifying the content type of the data you are sending - so Jersey is not able to locate the right MessageBodyWritter to serialize the b1 object.

Modify the last line of your main method as follows:

ClientResponse response = resource.type(MediaType.APPLICATION_XML).put(ClientResponse.class, b1);

And add @XmlRootElement annotation to class B on both the server as well as the client sides.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download