我正在配置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.resource.*)名称扫描而不必添加每个资源?这似乎是可能的jaxrs,但不是resteasy
发布于 2015-07-24 17:02:06
这些文件非常清楚:
要注册的完全限定JAX-RS资源类名的逗号分隔列表。
您可以自己使用倒影库来实现这一点。假设以下文本文件:
com.foo.bar.TestResource
com.foo.baz.*我们可以在应用程序类中读取这个文本文件,搜索所有类并将其添加到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;
}
}注意:我们只是在类级别上添加带有@Path注释的资源。
发布于 2015-07-24 15:53:09
我不是jaxrs的专家,但你查过下面的那些吗?
resteasy.scan
resteasy.scan.resources https://stackoverflow.com/questions/31614046
复制相似问题