ApplicationPath 被 wildfly 忽略

ApplicationPath being ignored by wildfly

我目前正在尝试设置我的第一个 Java-EE (JDK8) 应用程序,但无法覆盖上下文根以使用 @ApplicationPath 注释。 正在使用的应用程序服务器是 Wildfly 16.0.0.Final,我正在尝试使用 Resteasy 作为 JAX-RS 实现。

因为我使用 gradle 将我的应用程序部署为 war 这是我的依赖项:(这是一团糟,我试图弄清楚我真正需要什么,什么不需要, 欢迎随时提出建议 )

    dependencies {
    compileOnly 'org.jboss.logging:jboss-logging:3.3.0.Final'
    compileOnly 'org.apache.logging.log4j:log4j-api:2.8.2'
    compileOnly 'org.eclipse.persistence:javax.persistence:2.1.0'
    compileOnly 'javax.inject:javax.inject:1'
    compileOnly 'javax.transaction:javax.transaction-api:1.2'
    compileOnly 'javax.enterprise:cdi-api:2.0'
    compileOnly 'org.hibernate:hibernate-core:5.4.2.Final'
    compileOnly 'org.hibernate:hibernate-envers:5.4.2.Final'
    compileOnly 'com.fasterxml.jackson.core:jackson-core:2.9.8'
    compileOnly 'com.fasterxml.jackson.core:jackson-annotations:2.9.8'
    compileOnly 'com.fasterxml.jackson.core:jackson-databind:2.9.8'
    compileOnly 'javax.servlet:javax.servlet-api:3.1.0'
    compile group: 'org.jboss.resteasy', name: 'resteasy-jaxrs', version: '3.6.3.Final'
    compile group: 'org.jboss.resteasy', name: 'jaxrs-api', version: '3.0.12.Final'
}

我确认 wildfly 使用的是适当版本的 resteasy,它也使用 3.6.3。

我的应用程序配置如下所示:

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

@ApplicationPath("rest/*")
public class ApplicationConfig extends Application {
    // Intentionally empty. Just used to configure the application path for wildfly

    @Override
    public Set<Class<?>> getClasses() {
        final Set<Class<?>> s = new HashSet<>();
        s.add(HelloWorldController.class);
        return s;
    }

}

现在我目前能够成功部署我的应用程序,但上下文根仍然设置为我的 war 文件的名称,如下所示:

18:39:47,586 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 85) WFLYUT0021: Registered web context: '/Rest-Test-1.0' for server 'default-server'

当使用 war 名称访问 URL 时,我可以看到我的 WebFilter 的日志条目,但过滤器也无法正确执行链以将其寻址到正确的位置Class 带有匹配的 @Path 注释。(这可能也是 CDI 的问题吗?)

更新一: 将 @ApplicationPath 更改为“/rest”或 "rest" 也不会更改上下文根注册。它仍然采用 war 名称作为上下文根。

长话短说: 为什么我的 @ApplicationPath 注释被忽略了,为什么我的 webfilter 不能因此链接请求?

解法:

  1. 将依赖从 resteasy 更改为 compileOnly

这删除了 ​​warning WELD-000167: Class org.jboss.resteasy.core.AsynchronousDispatcher is annotated with @RequestScoped but it does not declare an appropriate constructor therefore is not registered as a bean!

  1. 将 ApplicationPath 更改为正确的路径(“/rest”)

这使我的 URI 正确注册为根并使 WebFilter 能够执行正确的链,因为它现在可以与 URI 中的每个组件区分开来。

  1. 添加jboss-web.xml到WEB-INF

与 jboss-web.xml 像这样:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web version="10.0"
           xmlns="http://www.jboss.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-web_10_0.xsd">

    <context-root>/api</context-root>
    <security-domain>other</security-domain> <!-- Configure to the security domain used for your deployed application -->
    <server-instance>default-server</server-instance>
    <virtual-host>default-host</virtual-host>

</jboss-web>

我能够将上下文根覆盖为一个更漂亮的根。

如果您的 IDE 声明了服务器实例元素的错误,您可以忽略它,它仍然可以正常工作。

您可以使用 application xml

更改上下文根

在发布我的解决方案之前,我首先要感谢 ogulcan 和 Sebastian S,因为他们的帮助让我找到了正确的解决方案。

解法:

  1. 将依赖从 resteasy 更改为 compileOnly

这删除了警告 WELD-000167:Class org.jboss.resteasy.core.AsynchronousDispatcher 用 @RequestScoped 注释但它没有声明适当的构造函数因此没有注册为 bean!

  1. 将 ApplicationPath 更改为正确的路径(“/rest”)

这使我的 URI 正确注册为根并使 WebFilter 能够执行正确的链,因为它现在可以与 URI 中的每个组件区分开来。

  1. 添加jboss-web.xml到WEB-INF

与 jboss-web.xml 这样:

http://www.jboss.org/j2ee/schema/jboss-web_10_0.xsd">

<context-root>/api</context-root>
<security-domain>other</security-domain> <!-- Configure to the security domain used for your deployed application -->
<server-instance>default-server</server-instance>
<virtual-host>default-host</virtual-host>

我能够将上下文根覆盖为更漂亮的根。

如果您的 IDE 声明了服务器实例元素的错误,您可以忽略它,它仍然可以正常工作。