按包名称扫描resteasy资源 [英] scan resteasy resources by package name

查看:340
本文介绍了按包名称扫描resteasy资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在配置我的resteasy应用程序,需要开启自动扫描(两个jaxrs应用程序在类路径,它在加载时断开)



因此,已配置我的web.xml如下:

 < context-param& 
< param-name> resteasy.scan< / param-name>
< param-value> false< / param-value>
< / context-param>

< context-param>
< param-name> resteasy.resources< / param-name>
< param-value>
io.swagger.jaxrs.listing.ApiListingResource,
com.mycompany.resource.ClientResource,
com.mycompany.resource.AccountResource,
... etc
< / param-value>
< / context-param>

在resteasy中有任何方法可以扫描包( com.mycompany。资源。* )名称,而不必添加每个资源?

解决方案

文档很清楚:


要注册的完全限定的JAX-RS资源类名称的逗号分隔列表


您可以自行实现此功能,例如反射库。假设以下文本文件:

  com.foo.bar.TestResource 
com.foo.baz。*
我们可以在Application类中读取这个文本文件,搜索所有类,并将它添加到<$ c返回的集合中 getClasses

  @ApplicationPath(/)
public class RestApplication extends应用程序{

Set< Class<?>>类;

public RestApplication(@Context ServletContext servletContext){
classes = new HashSet<>();
try {
URI resourcesConfig = servletContext.getResource(/ WEB-INF / resources.txt)。toURI();
List< String> resources = Files.readAllLines(Paths.get(resourcesConfig),Charset.forName(UTF-8));
for(String resource:resources){
parseResources(resource);
}
} catch(IOException | URISyntaxException | ClassNotFoundException ex){
throw new IllegalArgumentException(Could not add resource classes,ex);
}
}

private void parseResources(String resource)throws ClassNotFoundException,IOException {
if(!resource.endsWith(。*)){
classes.add(Class.forName(resource));
return;
}
String pkg = resource.substring(0,resource.length() - 2);
反射反射=新反射(pkg);
for(Class<?> scannedResource:reflections.getTypesAnnotatedWith(Path.class)){
classes.add(scannedResource);
}
}

@Override
public Set< Class<?>> getClasses(){
return classes;
}

}

注意:资源在类级别上具有 @Path 注释。


I'm configuring my resteasy app and need to turn of auto scanning (two jaxrs applications are in the classpath and it breaks at loading)

For that reason I have configured my web.xml as follow:

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

<context-param>
    <param-name>resteasy.resources</param-name>
    <param-value>
        io.swagger.jaxrs.listing.ApiListingResource,
        com.mycompany.resource.ClientResource,
        com.mycompany.resource.AccountResource,
        ... etc
    </param-value>
</context-param>

Is there any way in resteasy to scan by package (com.mycompany.resource.*) name instead of having to add each resource ? It seems it's possible with jaxrs but not resteasy

解决方案

The documentation is quite clear:

A comma delimited list of fully qualified JAX-RS resource class names you want to register

You could implement this on your own using for instance the reflections library. Assuming following textfile:

com.foo.bar.TestResource
com.foo.baz.*

We can read this textfile in the Application class, search for all classes and add it to the Set returned by getClasses:

@ApplicationPath("/")
public class RestApplication extends Application {

    Set<Class<?>> classes;

    public RestApplication(@Context ServletContext servletContext) {
        classes = new HashSet<>();
        try {
            URI resourcesConfig = servletContext.getResource("/WEB-INF/resources.txt").toURI();
            List<String> resources = Files.readAllLines(Paths.get(resourcesConfig), Charset.forName("UTF-8"));
            for (String resource : resources) {
                parseResources(resource);
            }
        } catch (IOException | URISyntaxException | ClassNotFoundException ex) {
            throw new IllegalArgumentException("Could not add resource classes", ex);
        }
    }

    private void parseResources(String resource) throws ClassNotFoundException, IOException {
        if (!resource.endsWith(".*")) {
            classes.add(Class.forName(resource));
            return;
        }
        String pkg = resource.substring(0, resource.length() - 2);
        Reflections reflections = new Reflections(pkg);
        for (Class<?> scannedResource : reflections.getTypesAnnotatedWith(Path.class)) {
            classes.add(scannedResource);
        }
    }

    @Override
    public Set<Class<?>> getClasses() {
        return classes;
    }

}

NOTE: We're only adding resources with a @Path annotation on the class level.

这篇关于按包名称扫描resteasy资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆