JAX-RS Jersey/Grizzly Define an interface resource

让人想犯罪 __ 提交于 2019-12-08 04:44:39

问题


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 interface 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.


回答1:


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.



来源:https://stackoverflow.com/questions/6727747/jax-rs-jersey-grizzly-define-an-interface-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!