JAX-RS Jersey/Grizzly Define an interface resource
Following the example here On deploying a sample resource using the Grizzly container. It uses a resource that is defined as a class, instead I would like to define an i开发者_运维问答nterface with the annotations and have the resource class implements that interface.
The problem now is that Grizzly complains that it can't find the resource:
com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
On Main class, where "com.mycompany.pack" is the package containing the implementation class:
final String baseUri = "http://localhost:9999/";
final Map<String, String> initParams = new HashMap<String, String>();
initParams.put("com.sun.jersey.config.property.packages", "com.mycompany.pack");
[Edit]: It works however when adding the annotations on the class as well. If there is a way to have the annotations declared only at the interface level.
You can't do it with package scanning because that only looks for classes with the JAX-RS annotations on them. You'll have to use a different approach: either one of the configuration options mentioned in the Jersey user guide that lets you explicitly declare your resource classes, or you could also use jersey-spring to manage your instances. With jersey-spring, there are no extra steps to be able to use an interface like you want to. You just annotate the interface, make the implementation a Spring bean, and it works.
精彩评论