java JAX-RS:如何扩展应用程序类来扫描包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26709288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:27:01  来源:igfitidea点击:

JAX-RS: How to extend Application class to scan packages?

javarestjerseyjax-rsresteasy

提问by daydreamer

Currently, I do something similar to

目前,我做类似的事情

import javax.annotation.Nonnull;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

@ApplicationPath("oauth")
public class OAuthApplication extends Application {
    final Set<Class<?>> classes = new HashSet<>();

    @Nonnull
    @Override
    public Set<Class<?>> getClasses() {
        classes.add(RegisterResource.class);
        return Collections.unmodifiableSet(classes);
    }
}

No if I add ten new Resources on the ApplicationPath, I need to do

不,如果我增加十个新Resource的S ApplicationPath,我需要做的

    classes.add(<ClassName>.class);

ten times, it is tedious and sometimes forgetful as well.

十次,它是乏味的,有时也是健忘的。

Does JAX-RSor RESTEasyprovide the way so that I can mention the package name and classes are scanned under it?

是否JAX-RSRESTEasy提供了一种方式,以便我可以提及包名称并在其下扫描类?

I know Jerseyhas something as

我知道Jersey有一些东西

public class MyApplication extends ResourceConfig {
    public MyApplication() {
        packages("org.foo.rest;org.bar.rest");
    }
}

Reference

参考

Any thoughts/ideas?

任何想法/想法?

UPDATE

更新

Seems we can do following in web.xml

似乎我们可以做以下 web.xml

   <context-param>
      <param-name>resteasy.scan</param-name>
      <param-value>true</param-value>
   </context-param>

Is there a specific Javaequivalent?

有具体的Java等价物吗?

回答by Paul Samsotha

"Is there a specific Java equivalent?"

“是否有特定的 Java 等价物?”

Simply leave the class empty, meaning do not override getClasses()or getSingletons(). Per the spec - 2.3.2:

只需将类留空,这意味着不要覆盖getClasses()getSingletons()根据规范 - 2.3.2

[...]

In either of the latter two cases, if both Application.getClassesand Application.getSingletonsreturn an empty list then all root resource classes and providers packaged in the web application MUST be included in the published JAX-RS application. If either getClassesor getSingletonsreturn a non-empty list then only those classes or singletons returned MUST be included in the published JAX-RS application.

[...]

在任何一种情况后两种情况下,如果同时Application.getClassesApplication.getSingletons返回一个空列表,然后打包在Web应用程序根目录的所有资源类和供应商必须被包含在发布的JAX-RS应用程序。如果getClassesgetSingletons返回一个非空列表,那么只有那些返回的类或单例必须包含在发布的 JAX-RS 应用程序中。

So you can simply do

所以你可以简单地做

@ApplicationPath("oauth")
public class OAuthApplication extends Application {}

and your classpath will get scanned for @Pathand @Providerclasses. Override either method (returning a non-empty set), and only those classes will be added.

并且您的类路径将被扫描@Path@Provider类。覆盖任一方法(返回非空集),并且只会添加那些类。

It should also be noted that public Map<String, Object> getProperties()can be safely overridden. You can use this to add arbitrary properties (even to register classes/features), as seen here, or if you need to configure a provider, you can do so in a Featureor DynamicFeatureas seen here

还应该注意的是,public Map<String, Object> getProperties()可以安全地覆盖。您可以使用它来添加任意属性(甚至注册类/功能),如此处所示,或者如果您需要配置提供程序,您可以在 aFeatureDynamicFeature如此处所示

回答by lalokana

Is a combination of all Application class, ResourceClass and @ApplicationPath

是所有Application类、ResourceClass和@ApplicationPath的组合

in agreement with the docsand the class ResourceConfigimplements by jersey the way to do is:

文档一致,并且类ResourceConfig由 jersey 实现,方法是:

@ApplicationPath(value = "/resource")
public class ApplicationREST extends ResourceConfig {

    public ApplicationREST() {
        //add single resources
        register(SimpleResource1.class);
        register(SimpleResource2.class);
        ...

        //enable multipar feature
        register(MultiPartFeature.class);
        ...            

        //enable scan package and recursive
        packages(true, "com.example.rest");
    }
}

I hope this helps you

我希望这可以帮助你

回答by Vikas Mittal

It is very simple:

这很简单:

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/webapi")
public class MyApp extends Application {

}

This will scan all the classes annotated with @Provider and @Path.

这将扫描所有用@Provider 和@Path 注释的类。