JAX-RS Jersey/Grizzly定义接口资源 [英] JAX-RS Jersey/Grizzly Define an interface resource
问题描述
以下示例为此处在部署示例时使用Grizzly容器的资源. 它使用定义为类的资源,而不是我想用批注定义一个接口,并让资源类实现该接口.
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.
在Main类上,其中"com.mycompany.pack"是包含实现类的软件包:
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");
:但是,当在类上添加注释时,也可以使用. 如果有一种方法可以仅在接口级别声明注释.
: 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.
推荐答案
您无法通过程序包扫描来完成此操作,因为它只会查找带有JAX-RS批注的类.您必须使用不同的方法:配置选项,可让您显式声明资源类,也可以使用球衣弹簧来管理您的实例.使用jersey-spring,不需要任何额外的步骤就可以使用您想要的界面.您只需注释接口,使实现成为Spring bean,它就可以工作.
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.
这篇关于JAX-RS Jersey/Grizzly定义接口资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!