Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 @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?

like image 976
skip Avatar asked Oct 20 '11 07:10

skip


1 Answers

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.

like image 106
Martin Matula Avatar answered Oct 15 '22 23:10

Martin Matula