I am creating an embedded Jetty webapp with Jersey. I do not know how to add Jackson for automatic JSON serde here:
ServletHolder jerseyServlet = context.addServlet(
org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter(
ServerProperties.PROVIDER_CLASSNAMES,
StringUtils.join(
Arrays.asList(
HealthCheck.class.getCanonicalName(),
Rest.class.getCanonicalName()),
";"));
// Create JAX-RS application.
final Application application = new ResourceConfig()
.packages("com.example.application")
.register(JacksonFeature.class);
// what do I do now to tie this to the ServletHolder?
How do I register this ResourceConfig
with the ServletHolder so Jackson with be used where the annotation @Produces(MediaType.APPLICATION_JSON)
is used? Here is the full main class for the embedded Jetty application
package com.example.application.web;
import com.example.application.api.HealthCheck;
import com.example.application.api.Rest;
import com.example.application.api.Frontend;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import javax.ws.rs.core.Application;
import java.util.Arrays;
public class JettyStarter {
public static void main(String[] args) throws Exception {
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
Server jettyServer = new Server(9090);
jettyServer.setHandler(context);
ServletHolder jerseyServlet = context.addServlet(
org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter(
ServerProperties.PROVIDER_CLASSNAMES,
StringUtils.join(
Arrays.asList(
HealthCheck.class.getCanonicalName(),
Rest.class.getCanonicalName()),
";"));
// Create JAX-RS application.
final Application application = new ResourceConfig()
.packages("com.example.application")
.register(JacksonFeature.class);
try {
jettyServer.start();
jettyServer.join();
} catch (Exception e) {
System.out.println("Could not start server");
e.printStackTrace();
} finally {
jettyServer.destroy();
}
}
}
One way is to just wrap the
ResourceConfig
in an explicit construction of theServletContainer
, as seen here.Tested with your example
You could also...
without changing anything else in your original post, just set the init param to scan the Jackson provider package
Note your attempted use of
ResourceConfig
seems a little redundant, as you are already configuring your classes in the the init param. You could alternatively get rid of adding each class explicitly and just scan entire packages as I have done.You could also...
just use the Jackson provider classes you need. You can look in the jar, and you will see more than just the marshalling/unmarhalling provider (Jackson[JAXB]JsonProvider), like a ExceptionMappers. You may not like these mappers and wand to configure your own. In which case, like I said, just include the provider you need. For example
After further testing...
Not sure what version of Jersey, but I am using Jersey 2.15 (with
jersey-media-json-jackson:2.15
), and without any further configuration from just scanning my package for my resource classes, the Jackson feature is already enabled. This is part of the auto discoverable features. I believe this was enable as of 2.8 or 2.9 for the Jackson feature. So if you are using a later one, I don't think you need to explicitly configure anything, at least from what I've tested :-)UPDATE
All of the above examples have been tested with the below Maven pom.xml
And resource class
Using path