我正在尝试用旧的 RESTEasy 3.0.5 将 WAR 更新到更新的版本。 3.0.6 工作正常,但在更新到 3.0.7(或更高版本,如 3.0.24)后,所有资源(@Path
)都丢失了——任何资源都是 404。 WAR 在 Apache Tomcat 服务器下运行。
我认为原因与注释扫描仪的变化有关: https://issues.jboss.org/browse/RESTEASY-1010
我尝试创建类来扩展 javax.ws.rs.core.Application
而不是 web.xml 配置。根据回答https://stackoverflow.com/a/29957040/2528366 ,空集应该触发对 @Path
的扫描,但未找到任何资源。如果我覆盖返回非空集的 getClasses()
,该资源将按预期工作。
web.xml:https://pastebin.com/uRD2w6Z6
新建Application
继承类:
@ApplicationPath("/rest")
public class WebApi extends Application
{
@Override
public Set<Class<?>> getClasses()
{
Set<Class<?>> s = new HashSet<>();
// if line below is uncommented SomeResource works fine
// s.add(SomeResourceImpl.class);
return s;
}
}
资源是接口(interface),实现在派生类中。将注释移至类本身不会改变任何内容。
注释或配置有什么问题?或者是否需要其他东西来触发扫描注释?
最佳答案
如果您使用的是 Tomcat version兼容Servlet 3.0规范,需要添加resteasy-servlet-initializer
依赖:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>${resteasy.version}</version>
</dependency>
如文档中所述:
Resteasy uses the ServletContainerInitializer integration interface in Servlet 3.0 containers to initialize an application, automatically scanning for resources and providers. To enable automatic scanning, you must also include the resteasy-servlet-initializer artifact in your WAR file as well
关于java - RESTEasy注解扫描找不到资源(Tomcat下),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46608898/