"网络容器尚未启动"通过jax-rs

时间:2016-05-06 11:45:52

标签: hibernate java-ee glassfish jax-rs glassfish-4

问题:

我试图实现一个简单的jax-rs服务,它将持久化作为jsons传递的实体。
我使用的堆栈的一部分是:

  • GlassFish 4.0
  • Jersey 2.0
  • Hibernate 5.1.0.Final
  • Oracle jdbc 12.1.0.2.0

目前我在尝试向服务发送任何有意义的http请求时收到IllegalStateException: This web container has not yet been started

StandardWrapperValve[com.my.lab.web.BookStoreApplication]: Servlet.service() for servlet com.my.lab.web.BookStoreApplication threw exception java.lang.IllegalStateException: This web container has not yet been started
    at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1652)
    at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1611)
    at oracle.jdbc.driver.T4CDriverExtension.allocatePreparedStatement(T4CDriverExtension.java:69)
    at oracle.jdbc.driver.PhysicalConnection.prepareStatementInternal(PhysicalConnection.java:2013)
    at oracle.jdbc.driver.PhysicalConnection.prepareStatement(PhysicalConnection.java:1960)
    at oracle.jdbc.OracleConnectionWrapper.prepareStatement(OracleConnectionWrapper.java:215)
    at com.sun.gjc.spi.base.ConnectionHolder.prepareStatement(ConnectionHolder.java:586)
    at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareCachedStatement(ConnectionWrapper40.java:255)
    at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareCachedStatement(ConnectionWrapper40.java:52)
    at com.sun.gjc.spi.ManagedConnectionImpl.prepareCachedStatement(ManagedConnectionImpl.java:992)
    at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareStatement(ConnectionWrapper40.java:173)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$1.doPrepare(StatementPreparerImpl.java:87)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:72)
    at org.hibernate.id.IncrementGenerator.initializePreviousValueHolder(IncrementGenerator.java:111)
    at org.hibernate.id.IncrementGenerator.generate(IncrementGenerator.java:52)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:105)
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67)
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:778)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:751)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:756)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1146)
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:287)
    at com.my.lab.dao.db.AbstractDbDao.saveEntity(AbstractDbDao.java:20)
    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:497)
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695)
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46)
    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:497)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
    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:497)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4667)
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4655)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
    at com.sun.proxy.$Proxy352.saveEntity(Unknown Source)
    at com.my.lab.dao.db.__EJB31_Generated__DbBookDao__Intf____Bean__.saveEntity(Unknown Source)
    at com.my.lab.web.resource.BookResource.saveNewBook(BookResource.java:60)
    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:497)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:125)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:152)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:91)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:346)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:341)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:101)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:224)
    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:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:198)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:946)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:323)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at com.my.lab.web.setting.swagger.SwaggerFilter.doFilter(SwaggerFilter.java:20)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:745)

<小时/>

实现:

到目前为止,我有一个资源类,将dao bean注入EJB(你也可以在那里看到一些招摇注释):

@Path("/book")
@Produces({MediaType.APPLICATION_JSON})
@Api(value = "book")
public class BookResource {

    @EJB
    private DbBookDao bookDao;


    @POST
    @Consumes({MediaType.APPLICATION_JSON})
    @ApiOperation(value = "Post a book", response = Book.class)
    @ApiResponses(value = {
            @ApiResponse(code = 409, message = "A book with such id is already exist"),
            @ApiResponse(code = 500, message = "Internal Server Error")})
    public Response saveNewBook(Book book) {
        Integer id;
        if (id = book.getId()) {
            throw new WebApplicationException(String.format("A book with id %d is already exist", id),
                    Response.Status.CONFLICT);
        }
        return Response.status(Response.Status.CREATED).entity(bookDao.saveEntity(book)).build();
    }

}

资源注册如下:

@ApplicationPath("/bookstore/api")
public class BookStoreApplication extends ResourceConfig {

    public BookStoreApplication() {
        initSwagger();
        packages("com.my.lab","io.swagger.jaxrs.json", "io.swagger.jaxrs.listing");
    }

    private void initSwagger() {
        BeanConfig swaggerConfig = new BeanConfig();
        swaggerConfig.setSchemes(new String[]{"http", "https"});
        // other BeanConfig methods
    }
}

这是dao bean类:

@Stateless
@LocalBean
public class DbBookDao extends AbstractDbDao<Book> {

    @Override
    protected Class<Book> getEntityClass() {
        return Book.class;
    }
}

扩展了一个包含实体管理器的抽象类:

public abstract class AbstractDbDao<T extends Entity> implements DAO<T> {

    @PersistenceContext(unitName = "demo-persistence-unit")
    private EntityManager entityManager;

    protected abstract Class<T> getEntityClass();

    @Override
    public T saveEntity(T entity) {
        entityManager.persist(entity);
        return entity;
    }
}

DAO界面是一个简单的合同,没有任何注释或者无论如何 最后这里是Book json / hibernate实体类:

@javax.persistence.Entity
@Table(name = "BOOKS")
public class Book implements Entity {

    @Id
    @GeneratedValue(generator = "book_counter")
    @GenericGenerator(name = "book_counter", strategy = "increment")
    @Column(name = "BOOK_ID")
    private Integer bookId;

    @NotNull
    @Column(name = "BOOK_NAME")
    @Basic(optional = false)
    private String name;

    @ManyToOne
    @JoinColumn(name = "AUTHOR", foreignKey = @ForeignKey(name = "AUTHOR_FK"))
    private Author author;

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "BOOK_GENRES",
            joinColumns = @JoinColumn(name = "GENRE_ID"))
    @Enumerated(EnumType.STRING)
    private List<Genre> genres;

    @Temporal(TemporalType.DATE)
    private Date written;


    public Book(String name) {
        this.name = name;
    }

    public Book() {
        // Default empty constructor for hibernate and JSON data binding
    }

    public Integer getId() {
        return bookId;
    }

    public void setId(Integer id) {
        this.bookId = id;
    }

    // other getters and setters
}

实施实体接口:

public interface Entity {
    Integer getId();
    void setId(Integer id);
}

<小时/>

CONFIGS:

以下是我的persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="demo-persistence-unit" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>jdbc/oracleDB</jta-data-source>

        <class>com.my.lab.business.entity.Book</class>
        <class>com.my.lab.business.entity.Author</class>

        <exclude-unlisted-classes>false</exclude-unlisted-classes>

        <properties>
            <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle12cDialect"/>
            <property name="hibernate.classloading.use_current_tccl_as_parent" value="false" />
            <property name="hibernate.flushMode" value="FLUSH_AUTO" />
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name='hibernate.hbm2ddl.auto' value="create"/>
        </properties>
    </persistence-unit>
</persistence>

和web.xml:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <filter>
        <filter-name>SwaggerFilter</filter-name>
        <filter-class>com.my.lab.web.setting.swagger.SwaggerFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>SwaggerFilter</filter-name>
        <url-pattern>/bookstore/api/*</url-pattern>
    </filter-mapping>
</web-app>

<小时/> 应用程序部署顺利,并且成功创建了所需的所有数据库表。但是在任何请求中,hibernate生成的查询执行都会在这个地方失败:

at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1652)
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1611)
at oracle.jdbc.driver.T4CDriverExtension.allocatePreparedStatement(T4CDriverExtension.java:69)

为了避免这种情况,我添加了persistence.xml

<property name="hibernate.classloading.use_current_tccl_as_parent" value="false" />

这不是一个很大的帮助 此外,我的所有代码:资源,实体和dao bean,以及所有xml配置都打包在一场战争中。此外,这场战争包含 WEB-INF / lib 下的jdbs.jar。我试图将它添加到 glassfish / domains / domain1 / lib / databases ,这似乎没有任何区别。

据我所知,问题在于GlassFish类加载过程,尽管我可能错了。我完全被困在这里,我对这个问题有任何帮助。

1 个答案:

答案 0 :(得分:0)

我设法通过将hibernate版本降级为4.3.11.Final来绕过此问题,因此我必须将hibernate.dialect更改为org.hibernate.dialect.Oracle10gDialect。 之后我遇到了一个问题,即将新数据保存到旧表中,删除表修复它(有些数据类型不匹配,可能是由于方言差异造成的)。