我追踪了将JDK 8升级到9甚至10后遇到的问题,并创建了这个小样本进行复制。
如果我提出要求
curl -i -X OPTIONS http://localhost:8088/test/test
使用OpenJDK 8很好。在OpenJDK 9或10中(没有尝试11或7),出现异常。
org.glassfish.jersey.internal.Errors logErrors
WARNUNG: The following warnings have been detected: WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 2
javax.ws.rs.ProcessingException: Error creating a JAXBContext for wadl processing.
at org.glassfish.jersey.server.wadl.internal.WadlApplicationContextImpl.<init>(WadlApplicationContextImpl.java:120)
...
正在运行Debian 10.2
对GET的请求在所有JDK中都是可以的。
这是我的样品。我只需在maven-compiler-plugin中的pom.xml的末尾进行设置即可更改JDK(现在为10,对我来说仅适用于8)。
希望任何人都可以给我提示。谢谢!
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>MyTest</groupId>
<artifactId>MyTest</artifactId>
<packaging>jar</packaging>
<version>0.1</version>
<name>MyTest</name>
<url>http://maven.apache.org</url>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>2.29</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>10</source>
<target>10</target>
</configuration>
</plugin>
</plugins>
</build>
MyTest.java
package MyTest.MyTest;
import java.net.*;
import org.glassfish.grizzly.http.server.*;
import org.glassfish.jersey.grizzly2.httpserver.*;
import org.glassfish.jersey.jdkhttp.*;
import org.glassfish.jersey.logging.*;
import org.glassfish.jersey.server.*;
public class MyTest
{
public static void main (String [] args) throws Exception
{
final String uri = "http://localhost:8088/"; // listening
ResourceConfig rc = new ResourceConfig ();
rc.packages (MyTest.class.getPackage ().getName ());
rc.property (LoggingFeature.LOGGING_FEATURE_LOGGER_LEVEL, "INFO");
rc.property (LoggingFeature.LOGGING_FEATURE_VERBOSITY_SERVER, LoggingFeature.Verbosity.PAYLOAD_TEXT);
// Grizzly-HTTP-Server
org.glassfish.grizzly.http.server.HttpServer hs = GrizzlyHttpServerFactory.createHttpServer (URI.create (uri), rc);
// tried JDK-HTTP as a 2nd container - but with ame effect problem
//com.sun.net.httpserver.HttpServer hs = JdkHttpServerFactory.createHttpServer (URI.create (uri), rc);
for (int i = 0; i < 100; i ++)
{
Thread.sleep (10000); // 10s
}
}
}
Handler.java
package MyTest.MyTest;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
@Path ("test")
public class Handler
{
@GET
@javax.ws.rs.Path ("/test")
@Produces (MediaType.TEXT_HTML)
public String list () throws Exception
{
System.out.println("test");
return "OK";
}
}
发现问题似乎出自JDK 9以来缺少的JAXB。
https://www.jesperdj.com/2018/09/30/jaxb-on-java-9-10-11-and-beyond/
如果添加依赖项,它将再次起作用。
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.2</version>
</dependency>