Tuesday, September 20, 2011

Guice + Jersey: The ResourceConfig instance does not contain any root resource classes

This happens because Jersey cannot or doesn't know how to find Resource classes. Jersey has to be told the package name in which Resource classes are located. The most obvious way is to add an init-param to the web.xml file. But since I'm using Guice servlets, I wanted to do it in Guice-way.

Before adding, my Jersey configuration in configureServlets method looked like this (taken from here):
serve("/api/*").with(GuiceContainer.class, ImmutableMap.of(JSONConfiguration.FEATURE_POJO_MAPPING, "true"));

Map parameters = new HashMap();
parameters.put(JSONConfiguration.FEATURE_POJO_MAPPING, "true");
parameters.put("com.sun.jersey.config.property.packages", "com.examle.api");
serve("/api/*").with(GuiceContainer.class, parameters);

A more robust version without hardcoded values:
parameters.put(PackagesResourceConfig.PROPERTY_PACKAGES, com.examle.api.AnyResource.class.getPackage().getName());

No comments:

Post a Comment