Search code examples
javajerseyjax-rsjersey-2.0

Bug in Jersey JAX_RS with OPTIONS header in JDK > 8?


I traced a problem I got after upgrading my JDK 8 to 9 or even 10 and created this little sample to reproduce it.

If I do a request

curl -i -X OPTIONS http://localhost:8088/test/test

it is fine with OpenJDK 8. In OpenJDK 9 or 10 (did not try 11 or 7) I get an exception.

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)
...

Running Debian 10.2.

A request for GET would be ok in all JDKs.

Here is my sample. I change the JDK by simply setting and at the end of the pom.xml in the maven-compiler-plugin (now 10, works only with 8 for me).

Hope anyone can give me a hint. Thanks!

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";
 }
}

Solution

  • Found out that the problem seems to be in the missing JAXB since JDK 9.

    https://www.jesperdj.com/2018/09/30/jaxb-on-java-9-10-11-and-beyond/

    If I add a dependency it is working again.

    <dependency>
      <groupId>org.glassfish.jaxb</groupId>
      <artifactId>jaxb-runtime</artifactId>
      <version>2.3.2</version>
    </dependency>