I am using jersey, and I want to send (in a POST) a list of objects to the server. This is the scenario:
@XmlRootElement
class Myclass{
//some primitive attributes + empty constructor + getter/setters
}
MyClass is both on server and client side.
@XmlRootElement
class MyClasses{
private List<MyClass> classes = new ArrayList<MyClass>();
// put some MyClass into the list
}
class Sender{
MyClasses list = new MyClasses();
// after client initialization i want to send this list in a POST to server
WebResource service = client.resource(baseURI());
//I tried
service.type("application/xml").accept("application/xml").post(ClientResponse.class,list);
}
//on server side
@path(“/tosend”)
class receiver{
public Response posted(JAXBElement<MyClasses> vals){
//work with vals.getValue() as the list of all Objects
}
}
Unfortunately, I have this error :
ContainerRequest getEntity : A Message body reader for JAXBElement and JAXBElement
and MIME Type application/octet-stream was not found.
How can I fix that?
Are you sure your code looks exactly as written above? The exception suggests you are not setting the content type of the request. Don’t use JAXBElement, and make sure the content type of the request is set to application/xml. In your code snippet you seem to be doing it. But the exception says the media type is application/octet-stream. So either the code snippet does not match your real code or the exception is coming from a different section of the code or you have some filters that change the message headers before it reaches the
posted()method. Annotate the method with@Consumes(MediaType.APPLICATION_XML).Btw, you don’t need to use the MyClasses wrapper class. You can simply send List and it will work as well.