2016-10-03 70 views
-1

我有一個使用Jersey框架和maven的Rest應用程序。我的應用運行良好。現在我決定重新設計我的代碼,並儘可能使用Java 8功能。我開始使用Optional類。但是當我在代碼中使用Optional.ifPresent方法時,tomcat無法啓動Exception。 注意:雖然沒有編譯錯誤。對Jersey框架的Java 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>something</groupId> 
<artifactId>project_name</artifactId> 
<packaging>war</packaging> 
<version>1.0-SNAPSHOT</version> 
<name>my project</name> 
<url>http://maven.apache.org</url> 

<repositories> 
    <repository> 
     <id>maven2-repository.java.net</id> 
     <name>Java.net Repository for Maven</name> 
     <url>http://download.java.net/maven/2/</url> 
     <layout>default</layout> 
    </repository> 
</repositories> 

<dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.8.2</version> 
     <scope>test</scope> 
    </dependency> 

    <dependency> 
     <groupId>com.sun.jersey</groupId> 
     <artifactId>jersey-server</artifactId> 
     <version>1.8</version> 
    </dependency> 

    <dependency> 
     <groupId>org.mongodb</groupId> 
     <artifactId>mongodb-driver</artifactId> 
     <version>3.2.2</version> 
    </dependency> 
    <dependency> 
     <groupId>com.google.code.gson</groupId> 
     <artifactId>gson</artifactId> 
     <version>2.2.4</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>javax.servlet-api</artifactId> 
     <version>3.0.1</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/com.typesafe/config --> 
    <dependency> 
     <groupId>com.typesafe</groupId> 
     <artifactId>config</artifactId> 
     <version>1.3.0</version> 
    </dependency> 

</dependencies> 

<build> 
    <finalName>WhereBy</finalName> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <configuration> 
       <source>1.8</source> 
       <target>1.8</target> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

Api.java

@POST 
@Path("/logout") 
@Produces("application/json") 
public Response logoutUser(@FormParam("email") String email){ 
    int status = 400; 
    JsonObject json = new JsonObject(); 
    try{ 
     String msg; 
     if (email==null) 
      json.addProperty("error", "invalid parameters"); 
     else{ 
      Optional<UserSchema> user = UserDAO.getUserByCredentials(email, Optional.empty()); 
      //user.ifPresent(x -> UserDAO.updateIsActive(x.getEmail(), 0)); 
      if(user.isPresent()) { 
       status = 200; 
       UserDAO.updateIsActive(user.get().getEmail(), 0); //0 = inactive 
      }else 
       json.addProperty("error", "invalid user credentials"); 
     } 
    }catch(Exception e){ 
     logger.log(Level.SEVERE, e.getMessage()); 
     json.addProperty("error", "exception occured"); 
    } 
    return Response.status(status).entity(json.toString()).build(); 
} 

在這裏,你可以看到,我想打電話給一些功能,如果用戶對象不爲空。現在,這段代碼一切正常,但如果我使用ifPresent函數(在代碼中註釋掉),tomcat在啓動時會拋出異常。

Tomcat的本地主機的日誌:

03-Oct-2016 12:09:11.836 SEVERE [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log StandardWrapper.Throwable 
java.lang.ArrayIndexOutOfBoundsException: 52264 
    at org.objectweb.asm.ClassReader.readClass(Unknown Source) 
    at org.objectweb.asm.ClassReader.accept(Unknown Source) 
    at org.objectweb.asm.ClassReader.accept(Unknown Source) 
    at com.sun.jersey.spi.scanning.AnnotationScannerListener.onProcess(AnnotationScannerListener.java:133) 
    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) 
    at com.sun.jersey.core.spi.scanning.uri.FileSchemeScanner.scanDirectory(FileSchemeScanner.java:83) 
    at com.sun.jersey.core.spi.scanning.uri.FileSchemeScanner.scan(FileSchemeScanner.java:71) 
    at com.sun.jersey.core.spi.scanning.PackageNamesScanner.scan(PackageNamesScanner.java:223) 
    at com.sun.jersey.core.spi.scanning.PackageNamesScanner.scan(PackageNamesScanner.java:139) 
    at com.sun.jersey.api.core.ScanningResourceConfig.init(ScanningResourceConfig.java:80) 
    at com.sun.jersey.api.core.PackagesResourceConfig.init(PackagesResourceConfig.java:104) 
    at com.sun.jersey.api.core.PackagesResourceConfig.<init>(PackagesResourceConfig.java:78) 
    at com.sun.jersey.api.core.PackagesResourceConfig.<init>(PackagesResourceConfig.java:89) 
    at com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(WebComponent.java:700) 
    at com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(WebComponent.java:678) 
    at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:203) 
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373) 
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556) 
    at javax.servlet.GenericServlet.init(GenericServlet.java:158) 
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1227) 
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1140) 
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1027) 
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5038) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5348) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145) 
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725) 
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701) 
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717) 
    at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1696) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) 
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) 
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) 
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:484) 
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:433) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) 
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) 
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) 
    at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468) 
    at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76) 
    at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309) 
    at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401) 
    at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:323) 
    at sun.rmi.transport.Transport$1.run(Transport.java:200) 
    at sun.rmi.transport.Transport$1.run(Transport.java:197) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196) 
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568) 
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826) 
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:745) 

03-Oct-2016 12:09:11.836 SEVERE [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.StandardContext.loadOnStartup Servlet [jersey-serlvet] in web application [/WhereBy] threw load() exception 
java.lang.ArrayIndexOutOfBoundsException: 52264 
    at org.objectweb.asm.ClassReader.readClass(Unknown Source) 
    at org.objectweb.asm.ClassReader.accept(Unknown Source) 
    at org.objectweb.asm.ClassReader.accept(Unknown Source) 
    at com.sun.jersey.spi.scanning.AnnotationScannerListener.onProcess(AnnotationScannerListener.java:133) 
    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) 
    at com.sun.jersey.core.spi.scanning.uri.FileSchemeScanner.scanDirectory(FileSchemeScanner.java:83) 
    at com.sun.jersey.core.spi.scanning.uri.FileSchemeScanner.scan(FileSchemeScanner.java:71) 
    at com.sun.jersey.core.spi.scanning.PackageNamesScanner.scan(PackageNamesScanner.java:223) 
    at com.sun.jersey.core.spi.scanning.PackageNamesScanner.scan(PackageNamesScanner.java:139) 
    at com.sun.jersey.api.core.ScanningResourceConfig.init(ScanningResourceConfig.java:80) 
    at com.sun.jersey.api.core.PackagesResourceConfig.init(PackagesResourceConfig.java:104) 
    at com.sun.jersey.api.core.PackagesResourceConfig.<init>(PackagesResourceConfig.java:78) 
    at com.sun.jersey.api.core.PackagesResourceConfig.<init>(PackagesResourceConfig.java:89) 
    at com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(WebComponent.java:700) 
    at com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(WebComponent.java:678) 
    at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:203) 
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373) 
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556) 
    at javax.servlet.GenericServlet.init(GenericServlet.java:158) 
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1227) 
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1140) 
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1027) 
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5038) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5348) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145) 
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725) 
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701) 
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717) 
    at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1696) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) 
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) 
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) 
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:484) 
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:433) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) 
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) 
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) 
    at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468) 
    at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76) 
    at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309) 
    at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401) 
    at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:323) 
    at sun.rmi.transport.Transport$1.run(Transport.java:200) 
    at sun.rmi.transport.Transport$1.run(Transport.java:197) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196) 
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568) 
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826) 
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:745) 
+4

這個龐大的代碼與'Optional.ifPresent'有關嗎?你的代碼甚至沒有使用它。請製作[mcve]。 – Tunaki

+1

當我註釋掉Optional.ifPresent時,它工作正常。只有當我嘗試使用Optional.ifPresent –

+1

時,纔會顯示此錯誤。問題不是特定於可選,而是特定於lambdas和類文件格式。您似乎正在使用ASM的過時版本,它不理解Java 8. –

回答