I'm having trouble hunting down the the problem behind SEVERE: A message body writer for Java class java.lang.Boolean, and Java type class java.lang.Boolean, and MIME media type application/json was not found
.
Here's what I've tried:
- using
jersey-bundle
instead of these individual packages - POJOMappingFeature is already enabled
- Adding Jackson as a dependency
I'm unclear on what else I need to do. This runs fine locally, so I'm unclear on why a dependency or two are not being packaged with the rest of the application.
pom.xml
<jersey-version>1.17.1</jersey-version>
...
<!-- Jersey -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey-version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>${jersey-version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey-version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>${jersey-version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
</exclusions>
</dependency>
Jersey Servlet
//add jersey servlet support
ServletRegistration jerseyServletRegistration = ctx.addServlet("JerseyServlet", new SpringServlet());
jerseyServletRegistration.setInitParameter("com.sun.jersey.config.property.packages", "com.company.product.resource");
jerseyServletRegistration.setInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters", "com.company.product.resource.ResponseCorsFilter");
jerseyServletRegistration.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", Boolean.TRUE.toString());
jerseyServletRegistration.setInitParameter("com.sun.jersey.config.feature.DisableWADL", Boolean.TRUE.toString());
jerseyServletRegistration.setInitParameter("org.codehaus.jackson.map.DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY", Boolean.TRUE.toString());
jerseyServletRegistration.setLoadOnStartup(1);
My build process
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>shade</goal></goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.company.product.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
The exception:
SEVERE: A message body writer for Java class java.lang.Boolean, and Java type class java.lang.Boolean, and MIME media type application/json was not found
SEVERE: The registered message body writers compatible with the MIME media type are:
*/* ->
com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
Code that generates error:
@Path("/system")
public class SystemResource extends BaseResource {
@GET
@Path("/isOnline")
public Boolean isOnline () {
return Boolean.TRUE;
}
}
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public class BaseResource {
}
I've verified that via the package log that jersey-json is being included in my jar. One area of uncertainty for me is which package/class actually provides the message body writer for Boolean?
Bounty was added because this is preventing me from running in our production environment and we really need this resolved.
Try to include jackson also:
I assume that you're creating a one big
jar
file that contains your classes and all other dependencies. If this is correct (also the posted exception with only one provider leads me to this assumption) then make sure that alljavax.ws.rs.ext.MessageBodyReader
(andjavax.ws.rs.ext.MessageBodyWriter
) files from your dependencies are combined together and not just replaced by ones from the latest jar that has been added to you uber-jar.Make sure that your
javax.ws.rs.ext.MessageBodyWriter
file in the uber-jar (inMETA-INF\services
) contains providers from this file that is present injersey-json
:The last one is especially important in your case since you're using Jackson as your JSON provider (by setting
POJOMappingFeature
totrue
).EDIT 1
Many modules in Jersey 1.x uses
META-INF/services
mechanism to lookup for JAX-RS provides (i.e.MessageBodyReader
s, ...). This means that you can find a service file in a jar (i.e.jersey-json.jar!META-INF/services/javax.ws.rs.ext.MessageBodyReader
) in which are defined classes that Jersey runtime uses (i.e. to read/write entity from/to input/output). The issue with creating an uber-jar is that you also need to combine contents of same services (defined by the files with same name inMETA-INF/services/
in your dependencies) into one file and bundle it into your executable jar (i.e. combineMETA-INF/services/javax.ws.rs.ext.MessageBodyReader
fromjersey-core
andjersey-json
and store it inMETA-INF/services/javax.ws.rs.ext.MessageBodyReader
in your jar).In case of Jersey you can use
jersey-bundle
as mentioned elsewhere but make sure theMETA-INF/services
files fromjersey-bundle
are in your resulting jar.Overwriting of
META-INF/services
during jar creation seems to be the root cause (as described in the first answer). In my case adding:to the
<transformers>
config of the maven-shade-plugin in pom.xml has fixed the problem. Including a fulljersey-bundle
seems to be an overkill.No need to add 3rd-party libs because it has been included in jersey-json package. The code seems not right, and it should be: