MATLAB Answers

REST Web Service call via Java does not find message body reader (MIME octet-stream)

466 views (last 30 days)
Andy Hueni
Andy Hueni on 11 Mar 2013
We have developed a Java client/server application using REST and Jersey for the wrapping/unwrapping of data to be sent between client and server. This works fine within Eclipse as well as for the exported Jar file. We are now trying to use our Java client classes within Matlab to connect to the application server. This produces an error message related to the reader of the message body:
Mar 11, 2013 5:28:37 PM com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: A message body reader for Java class ch.specchio.types.User, and Java type class ch.specchio.types.User, and MIME media type application/octet-stream was not found
Mar 11, 2013 5:28:37 PM com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: The registered message body readers compatible with the MIME media type are:
application/octet-stream ->
*/* ->
com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class ch.specchio.types.User, and Java type class ch.specchio.types.User, and MIME media type application/octet-stream was not found
at com.sun.jersey.api.client.ClientResponse.getEntity(
at com.sun.jersey.api.client.ClientResponse.getEntity(
at com.sun.jersey.api.client.WebResource.handle(
at com.sun.jersey.api.client.WebResource.access$200(
at com.sun.jersey.api.client.WebResource$Builder.get(
at ch.specchio.client.SPECCHIOWebClient.getObject(
at ch.specchio.client.SPECCHIOWebClient.connect(
at ch.specchio.client.SPECCHIOClientFactory.connect(
at ch.specchio.gui.DatabaseConnectionDialog.actionPerformed(
at javax.swing.AbstractButton.fireActionPerformed(
at javax.swing.AbstractButton$Handler.actionPerformed(
at javax.swing.DefaultButtonModel.fireActionPerformed(
at javax.swing.DefaultButtonModel.setPressed(
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(
at java.awt.Component.processMouseEvent(
at javax.swing.JComponent.processMouseEvent(
at java.awt.Component.processEvent(
at java.awt.Container.processEvent(
at java.awt.Component.dispatchEventImpl(
at java.awt.Container.dispatchEventImpl(
at java.awt.Component.dispatchEvent(
at java.awt.LightweightDispatcher.retargetMouseEvent(
at java.awt.LightweightDispatcher.processMouseEvent(
at java.awt.LightweightDispatcher.dispatchEvent(
at java.awt.Container.dispatchEventImpl(
at java.awt.Window.dispatchEventImpl(
at java.awt.Component.dispatchEvent(
at java.awt.EventQueue.dispatchEventImpl(
at java.awt.EventQueue.access$400(
at java.awt.EventQueue$
at java.awt.EventQueue$
at Method)
at java.awt.EventQueue$
at java.awt.EventQueue$
at Method)
at java.awt.EventQueue.dispatchEvent(
at java.awt.EventDispatchThread.pumpOneEventForFilters(
at java.awt.EventDispatchThread.pumpEventsForFilter(
at java.awt.EventDispatchThread.pumpEventsForHierarchy(
at java.awt.EventDispatchThread.pumpEvents(
at java.awt.EventDispatchThread.pumpEvents(
It appears that the returned message body is no longer XML but an octet stream and this is presumably preventing the proper unwrapping by Jersey. Why this happens is beyond me. The jar files and class paths are set within the classpath.txt file:
/Users/andyhueni/Documents/workspace/SPECCHIO Types/bin
/Users/andyhueni/Documents/workspace/SPECCHIO Web Client/bin
/Users/andyhueni/Documents/workspace/SPECCHIO Web Client/lib/jersey-client.jar
/Users/andyhueni/Documents/workspace/SPECCHIO Web Client/lib/jersey-core.jar
Within Java, the classes, such as in this case User, have been annotated with the return type, ditto the web service methods:
public class User {
public User login() throws SPECCHIOFactoryException {
UserFactory factory = new UserFactory(getClientUsername(), getClientPassword());
User user = factory.getUser(getClientUsername());
return user;
Environment: - MATLAB R2011a - Eclipse Java EE, Juno Release 1 - Glassfish Server

Accepted Answer

Andy Hueni
Andy Hueni on 5 Apr 2013
Here is part 2 of the solution to the problem, which not strictly connected to Matlab, but also occurred when trying to call the services from R. You have to use the "copy required libraries into a sub-folder" option when exporting the JAR from Eclipse. The information that tipped us off to this solution is at:

More Answers (1)

Andy Hueni
Andy Hueni on 16 Mar 2013
Edited: Andy Hueni on 16 Mar 2013
I'm now in the position to answer part of the problem. The octect stream is the result of a missing header in the returned response. To figure this out, edit your Java code (if you got the source), to add a filter to your webservice. Optionally, you can also write the header and the body to a file (note that this somehow crashes the actual JAXB reading of the response, but no matter for this test). In that filter, the response type can then be forced to be "application/xml" by using a reflection. Doing so removes the 'octet stream' error.
It appears that somewhere in the calling stack, Matlab is setting the returned header to null; I have tried the same call from R, and there the header was preserved.
A remaining issue is that even so, no body readers are found for the xml either, i.e. the map of the known message body workers is presumably wrongly configured in the Jersey framework; how to configure that properly is my next endeavour.
The filter code looks like:
class MyClientFilter extends ClientFilter
public ClientResponse handle(ClientRequest cr)
throws ClientHandlerException {
// Call the next client handler in the filter chain
ClientResponse resp = getNext().handle(cr);
System.out.println("Reponse type: " + resp.getType());
System.out.println("Status : " + resp.getStatus());
boolean write_header_and_body_to_file = false;
if(write_header_and_body_to_file == true)
try {
FileWriter w = new FileWriter("/tmp/specchio_out.txt");
for(String key : resp.getHeaders().keySet())
List<String> s = resp.getHeaders().get(key);
w.write(key + " : ");
for(String str : s)
w.write(str + "\n");
InputStream buf = resp.getEntityInputStream();
while(buf.available() > 0) {
byte[] b = new byte[buf.available()];;
w.write(new String(b));
} catch (IOException e1) {
// TODO Auto-generated catch block
// create header
InBoundHeaders h = new InBoundHeaders();
h.add("Content-Type", "application/xml");
try {
// set content type header in response
Field HField = ClientResponse.class.getDeclaredField("headers");
HField.set(resp, h);
} catch (SecurityException e) {
// TODO Auto-generated catch block
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
return resp;
The filter is added to your web service by (just before you call the service):
MyClientFilter f = new MyClientFilter(); web_service.addFilter(f);
out = web_service.path(buildPath(service, method, args)).accept(MediaType.APPLICATION_XML).get(objectClass);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!