Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey and Java 8 (Lambda expression)

I use Jersey version 1.18.1 (com.sun.jersey), Spring and Java 8. If I put a Java 8 Lambda expression in a REST service, it crashes. If I remove the lambda expression, it works.

@Service
@Path("/hello")
public class Hello {

    @GET
    public String hello() {
        new ArrayList<String>().stream().filter((str) -> str.length() > 0);
        return "hello";
    }

}

I use com.sun.jersey (1.18.1 version).

Full stacktrace:

SEVERE: Allocate exception for servlet jersey-serlvet java.lang.ArrayIndexOutOfBoundsException: 52264
    at jersey.repackaged.org.objectweb.asm.ClassReader.readClass(ClassReader.java:1976)
    at jersey.repackaged.org.objectweb.asm.ClassReader.accept(ClassReader.java:464)
    at jersey.repackaged.org.objectweb.asm.ClassReader.accept(ClassReader.java:420)
    at com.sun.jersey.spi.scanning.AnnotationScannerListener.onProcess(AnnotationScannerListener.java:138)
    at com.sun.jersey.core.spi.scanning.uri.FileSchemeScanner$1.f(FileSchemeScanner.java:86)
    at com.sun.jersey.core.util.Closing.f(Closing.java:71)

Please tell me how to fix it.

like image 854
Malahov Avatar asked Jun 15 '14 18:06

Malahov


2 Answers

Jersey 1.18 is not compatible with java 8. They added java 8 compatibility since 1.19.

If you take a look at the Jersey 1.19 release notes, you can see the introduced the jdk8 support.

https://github.com/jersey/jersey-1.x/releases/tag/1.19

Quoting the 1.19 tag:

Thanks to @cowwoc you're able to run Jersey 1.x applications on JDK8

To fix this you have to upgrade Jersey to 1.19 or downgrade java 8 to 7.

like image 181
Federico Piazza Avatar answered Sep 23 '22 02:09

Federico Piazza


We had similar problem. Also com.sun.jersey in use.

After migration to the newest version (19.1) all works. So simply please upgrade to the newest version.

like image 29
Ziemowit Stolarczyk Avatar answered Sep 25 '22 02:09

Ziemowit Stolarczyk