java - JDK > 8 中带有 OPTIONS header 的 Jersey JAX_RS 中存在错误?

标签 java jersey jax-rs jersey-2.0

我跟踪了将 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>

关于java - JDK > 8 中带有 OPTIONS header 的 Jersey JAX_RS 中存在错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59227375/

相关文章:

java - 对于同一个 REST 方法,我们可以有多个 @Path 注释吗

java - 如何在 jax-rs Rest 服务中实现自定义的基于用户的授权?

java - 类路径在 linux 下不起作用

java - 从 java 对象创建复杂的 JSON

java - 我的通用 EnumSet 方法 - 无法访问 Enum.values()

java - 使用 ClientBuilder 发送 JSON 正文,但使用 ContentType=application/x-www-form-urlencoded

java - 在容器中等待 CountDownLatch 的多个实例

java - 自定义 ExceptionMapper 在 Spring + Jersey + Jetty 中不起作用

java - 带有 Jersey 和 tomcat 错误 : 404 的 Restful Web 服务

java - 使用构建器模式编码解码不可变对象(immutable对象)的最佳方法