org.glassfish.jersey.server.ContainerException:调用服务时的 java.lang.ExceptionInInitializerError

Posted

技术标签:

【中文标题】org.glassfish.jersey.server.ContainerException:调用服务时的 java.lang.ExceptionInInitializerError【英文标题】:org.glassfish.jersey.server.ContainerException: java.lang.ExceptionInInitializerError when calling service 【发布时间】:2014-05-19 17:58:39 【问题描述】:

在我的 maven 项目中,当我调用我的 web 服务时,我得到一个 java.lang.ExceptionInInitializerError

看来,生成响应时一定有问题。 我可以成功调用我的服务,但是在生成响应时,它似乎崩溃了。 一个简单的“Response.ok().build() 效果很好。但就我创建实体管理器而言 - 这与响应没有任何关系 - 它崩溃了......(我仍然只回复 200)。

方法体中引发错误的行是 'EntityManager em = EntityManagerSingleton.INSTANCE.create();'

public enum EntityManagerSingleton 
INSTANCE;

private static EntityManagerFactory singleton Persistence.createEntityManagerFactory(EntityKeyManager.DEBUG_ENTITY_MANAGER);
public EntityManager create() 
    return singleton.createEntityManager();

我猜有一些关于 jersey-container 的依赖问题,它以某种方式“混淆”了 maven... (Maven 全新安装没有帮助 ;-))

有人知道我的问题可能出在哪里吗?非常感谢您的帮助!

堆栈跟踪

org.glassfish.jersey.server.ContainerException: java.lang.ExceptionInInitializerError
at org.glassfish.jersey.servlet.internal.ResponseWriter.rethrow(ResponseWriter.java:249)
at org.glassfish.jersey.servlet.internal.ResponseWriter.failure(ResponseWriter.java:231)
at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:433)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:265)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:320)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:219)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1221)
at com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:156)
at com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:233)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:928)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

服务类

@Path("getAssignments")
public class GetAssignments 

@GET
public Response getAssignments() 

    List<Assignment> results = AssignmentUtil.findAll(); // here I generete the EntityManager as described above and make a simple sql native query
    List<AssignmentDto> resDtoList = new ArrayList<AssignmentDto>();
    for(Assignment ass : results)
        resDtoList.add(AssignmentTransformator.transform(ass));
    
    return Response.ok().build();

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
         xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="bachelordebug" transaction-type="RESOURCE_LOCAL">
    <!-- <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> -->
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
   <class>com.uzh.platform.data.dao.Assignment</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://127.0.0.1:5432/bachelor"/>
        <property name="javax.persistence.jdbc.user" value="bachelor"/>
        <property name="javax.persistence.jdbc.password" value="bachelor14"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
        <property name="hibernate.connection.release_mode" value="after_statement"/>
        <property name="connection.pool_size" value="1"/>
        <property name="dialect" value="org.postgresql.Driver"/>
        <property name="current_session_context_class" value="thread"/>
        <property name="cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
        <property name="show_sql" value="true"/>
    </properties>
</persistence-unit>

这是我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
    <artifactId>platform</artifactId>
    <groupId>com.uzh.platform</groupId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>


<groupId>com.uzh.platform.api</groupId>
<artifactId>api</artifactId>
<packaging>war</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<build>
    <finalName>rest</finalName>
    <plugins>
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>6.1.26</version>
            <configuration>
                <!--scan every X seconds for changes in source code and redeploy them automatically-->
                <scanIntervalSeconds>5</scanIntervalSeconds>
                <!-- enable stopping jetty server by jetty:stop -->
                <stopPort>9966</stopPort>
                <stopKey>foo</stopKey>
                <webXml>$webxml.path</webXml>
            </configuration>
        </plugin>
    </plugins>

</build>
<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.5.1</version>
    </dependency>
    <dependency>
        <groupId>com.wordnik</groupId>
        <artifactId>swagger-jaxrs_2.9.1</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.2.3</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.logging</groupId>
        <artifactId>jboss-logging</artifactId>
        <version>3.1.3.GA</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.9</version>
    </dependency>
    <dependency>
        <groupId>com.thetransactioncompany</groupId>
        <artifactId>cors-filter</artifactId>
        <version>1.7.1</version>
    </dependency>

    <dependency>
        <groupId>com.uzh.platform.core</groupId>
        <artifactId>core</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>1.9</version>
    </dependency>
</dependencies>

将球衣版本设置为 2.7 也无济于事......

【问题讨论】:

results 可能为空?在这种情况下,它将是 NPE。如果您明白我的意思是哪一个,还可以考虑重命名您的变量之一:) 嗨 Christophe - 我编辑了上面的描述......最有趣的是:在我制作 maven 构建或全新安装之前,一切都很完美...... 你知道 POM 发生了什么变化吗? (您可以查看历史或本地历史) 没有任何变化.... :/ 【参考方案1】:

我终于找到了我的错误!

在我的 JPA-Entity 类中,我拼错了我的命名查询注释(即使我不使用它)

@NamedQuery(name="Assignment.findAll", query ="SELECT * FROM assignment")

当然必须用大写字母写,即“Assignment

好尴尬……

感谢您的帮助!

【讨论】:

以上是关于org.glassfish.jersey.server.ContainerException:调用服务时的 java.lang.ExceptionInInitializerError的主要内容,如果未能解决你的问题,请参考以下文章