WildFly 12 和 Java10 不识别注解
WildFly 12 and Java 10 does not recognize annotations
我有什么
- Tomcat 9.0.7
- 野蝇 12.0.0
- JDK 10
- 行家 3.5.3
相关文件
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.training</groupId>
<artifactId>BigVoiceWebApp</artifactId>
<name>BigVoiceWebApp Maven Webapp</name>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>1.10</maven.compiler.source>
<maven.compiler.target>1.10</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<!-- <build>
<finalName>${project.artifactId}</finalName>
</build>-->
<profiles>
<profile>
<id>release-profile</id>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<finalName>BigVoiceWebApp</finalName>
</build>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>Archetype Created Web Application</display-name>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/PostgresDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
我的问题
我有这样的 servlet:
@WebServlet(name = "MemberController", urlPatterns = {"/MemberController"})
@MultipartConfig
public class MemberController extends HttpServlet {
现在的问题是,WildFly 不知何故不服从注解。当我访问 URL 时,这是我在浏览器中得到的(通过查看源 HTML 复制):
<html>
<head><title>Error</title></head>
<body>Not Found</body>
</html>
我必须在 web.xml
中为我声明为注释的内容声明所有内容(添加到之前的文件)。
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.example.training.controller.MemberController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/MemberController</url-pattern>
</servlet-mapping>
当我将完全相同的 WAR 文件重新上传到 Tomcat 时,注释被正确读取。
我的问题是,为什么?以及如何让 WildFly 识别这些注释?
感谢您提供文件。是 Java 10。我在构建过程中注意到:
[INFO] --- maven-war-plugin:2.2:war (default-war) @ SampleForError ---
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/home/scott/.m2/repository/com/thoughtworks/xstream/xstream/1.3.1/xstream-1.3.1.jar) to field java.util.Properties.defaults
WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Wildfly 启动时会产生类似的警告。 Wildfly 12 和 Java 10 的某些组合在 JEE7 模式下无法正常工作。
但是,有多种解决方法。如果你 运行 Wildfly in EE8 Preview Mode 那么它就可以了。通过 运行ning bin/standalone.sh -Dee8.preview.mode=true
或 bin/standalone.sh -c standalone-ee8.xml
执行此操作
否则,这在 Java 8.
中没有任何变化。
顺便说一句,您非常努力地试图混淆您 运行 所在的任何容器。您在 pom.xml
中指定了 servlet 规范 4,然后 web.xml是:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
因此您的 web.xml
告诉容器您正在使用 Servlet 规范 2.3。如果你真的需要 servlet 规范 4.0 那么你的 web.xml
应该是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>
我有什么
- Tomcat 9.0.7
- 野蝇 12.0.0
- JDK 10
- 行家 3.5.3
相关文件
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.training</groupId>
<artifactId>BigVoiceWebApp</artifactId>
<name>BigVoiceWebApp Maven Webapp</name>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>1.10</maven.compiler.source>
<maven.compiler.target>1.10</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<!-- <build>
<finalName>${project.artifactId}</finalName>
</build>-->
<profiles>
<profile>
<id>release-profile</id>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<finalName>BigVoiceWebApp</finalName>
</build>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>Archetype Created Web Application</display-name>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/PostgresDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
我的问题
我有这样的 servlet:
@WebServlet(name = "MemberController", urlPatterns = {"/MemberController"})
@MultipartConfig
public class MemberController extends HttpServlet {
现在的问题是,WildFly 不知何故不服从注解。当我访问 URL 时,这是我在浏览器中得到的(通过查看源 HTML 复制):
<html>
<head><title>Error</title></head>
<body>Not Found</body>
</html>
我必须在 web.xml
中为我声明为注释的内容声明所有内容(添加到之前的文件)。
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.example.training.controller.MemberController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/MemberController</url-pattern>
</servlet-mapping>
当我将完全相同的 WAR 文件重新上传到 Tomcat 时,注释被正确读取。
我的问题是,为什么?以及如何让 WildFly 识别这些注释?
感谢您提供文件。是 Java 10。我在构建过程中注意到:
[INFO] --- maven-war-plugin:2.2:war (default-war) @ SampleForError ---
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/home/scott/.m2/repository/com/thoughtworks/xstream/xstream/1.3.1/xstream-1.3.1.jar) to field java.util.Properties.defaults
WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Wildfly 启动时会产生类似的警告。 Wildfly 12 和 Java 10 的某些组合在 JEE7 模式下无法正常工作。
但是,有多种解决方法。如果你 运行 Wildfly in EE8 Preview Mode 那么它就可以了。通过 运行ning bin/standalone.sh -Dee8.preview.mode=true
或 bin/standalone.sh -c standalone-ee8.xml
否则,这在 Java 8.
中没有任何变化。顺便说一句,您非常努力地试图混淆您 运行 所在的任何容器。您在 pom.xml
中指定了 servlet 规范 4,然后 web.xml是:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
因此您的 web.xml
告诉容器您正在使用 Servlet 规范 2.3。如果你真的需要 servlet 规范 4.0 那么你的 web.xml
应该是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>