This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Web Services and the fly likes JAXB Based JSON support not working with Jersey JAX-RS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "JAXB Based JSON support not working with Jersey JAX-RS" Watch "JAXB Based JSON support not working with Jersey JAX-RS" New topic
Author

JAXB Based JSON support not working with Jersey JAX-RS

Chris Rzohli
Greenhorn

Joined: Jul 06, 2011
Posts: 2
Do I make something wrong using JAXB based JSON support with the Jersey 1.17 Client API?
The code below delivers

[code=java]
415
[text/html;charset=utf-8]
Mai 01, 2013 8:23:30 AM com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: A message body reader for Java class client.Pair, and Java type class client.Pair, and MIME media type text/html; charset=utf-8 was not found
Mai 01, 2013 8:23:30 AM com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: The registered message body readers compatible with the MIME media type are:
/ ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$General
com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
com.sun.jersey.core.impl.provider.entity.EntityHolderReader
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy

Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class client.Pair, and Java type class client.Pair, and MIME media type text/html; charset=utf-8 was not found
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:561)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:517)
at client.ComputeConcat.main(ComputeConcat.java:39)
[/code]

[code=java]
package server;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
*
* calling with data as JSON serialization instead of JAXB
* $ curl -X POST --data-binary "{ \"first\":\"Hello, \", \"second\":\"World.\" }" \
* -H "Content-Type: application/json" -H "Accept: application/json" \
* --user exampleuser:changeit \
* http://localhost:8080/JAXRSServer/rs/concatEngine/concat
* delivers
* { "first":"Hello, World." }
*/
@Path("/concatEngine")
public class ConcatEngine {
@POST
@Path("/concat")
// MediaType.APPLICATION_XML,
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public Pair concat(Pair data) {
Pair res = new Pair();
res.setFirst(data.getFirst() + data.getSecond());
return res;
}
}
[/code]

[code=java]
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Pair {
protected String first;
protected String second;

public Pair() {
}

public String getFirst() {
return first;
}

public void setFirst(String first) {
this.first = first;
}

public String getSecond() {
return second;
}

public void setSecond(String second) {
this.second = second;
}
}
[/code]

[code=java]
package client;

import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

public class ComputeConcat {

private static WebResource getHttpResource() {
ClientConfig config = new DefaultClientConfig();
// config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
// config.getClasses().add(JSONRootElementProvider.class);
Client client = Client.create(config);
return client.resource(
"http://localhost:8080/JAXRSServer/rs/concatEngine/concat");
}


public static void main(String[] args) {
Pair pair = new Pair();
pair.setFirst("Hello, "); pair.setSecond("World!");

WebResource resource = getHttpResource();

resource.addFilter(new HTTPBasicAuthFilter("exampleuser", "changeit"));
// resource.accept(MediaType.APPLICATION_XML);
resource.accept(MediaType.APPLICATION_JSON);
resource.type(MediaType.APPLICATION_JSON);
// resource.header("content-type", MediaType.APPLICATION_JSON);

ClientResponse response = resource.post(ClientResponse.class, pair);
System.out.println(response.getStatus());
System.out.println(response.getHeaders().get("Content-Type"));
Pair res = response.getEntity(Pair.class);
System.out.println(res.getFirst());
}

}
[/code]

My web.xml (under Tomcat) if that should be of interest:

[code=java]
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<display-name>JAXRSServer</display-name>

<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>server</param-value>
</init-param>
<!-- init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rs/*</url-pattern>
</servlet-mapping>
</web-app>
[/code]

f changing @Consumes({MediaType.APPLICATION_JSON}) to @Consumes({MediaType.APPLICATION_XML}) for the concat method (and MediaTypes), then the code operates. However, then the input pair is sent as XML and not as JSON serialization. It seems like the Jersy Client does not automatically serialize the pair object to JSON but only to XML, although imho it should.

Is there any bug database for Jersey where I can post if this should be an issue?

Any ideas? Thank you!
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 412
    
    3
1. Use TCP/MON or any rest client UI
2. Have a look at the response header + body.
Check this against your expectation.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JAXB Based JSON support not working with Jersey JAX-RS