无法在Tomcat中部署带有HK2的Jersey

时间:2019-04-01 19:16:27

标签: tomcat dependency-injection jersey jersey-2.0 hk2

我有一个工作的Jersey JAX-RS应用程序,我刚刚对其进行了修改,以在一个构造函数中注入一个依赖项。我尝试在GlassFish上部署它,但后来遇到了这个问题here

所以我考虑改用Tomcat,因为我认为这是HK2和CDI如何相互冲突的问题,但它在Tomcat上也不起作用,我在部署战争时遇到此错误文件:

  

严重[RMI TCP连接(2)-127.0.0.1] org.apache.catalina.startup.ContextConfig.beforeStart上下文的docBase异常修复[/ Gradle ___ com_myproject___webservice_1_0_0_0_SNAPSHOT_war]    java.io.IOException:无法创建目录[C:\ Program Files \ Apache Software Foundation \ Tomcat 8.5 \ webapps \ Gradle ___ com_myproject___webservice_1_0_0_SNAPSHOT_war]       在org.apache.catalina.startup.ExpandWar.expand(ExpandWar.java:115)       在org.apache.catalina.startup.ContextConfig.fixDocBase(ContextConfig.java:615)       在org.apache.catalina.startup.ContextConfig.beforeStart(ContextConfig.java:748)       在org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:301)       在org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)       在org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:395)       在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:149)       在org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)       在org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)       在org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)       在org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1736)       在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处       在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       在java.lang.reflect.Method.invoke(Method.java:498)       在org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)       在com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)       在com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)       在org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:482)       在org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:431)       在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处       在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       在java.lang.reflect.Method.invoke(Method.java:498)       在org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)       在com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)       在com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)       在com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:468)       在javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)       在javax.management.remote.rmi.RMIConnectionImpl.access $ 300(RMIConnectionImpl.java:76)       在javax.management.remote.rmi.RMIConnectionImpl $ PrivilegedOperation.run(RMIConnectionImpl.java:1309)       在java.security.AccessController.doPrivileged(本机方法)       在javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1408)       在javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)       在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处       在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       在java.lang.reflect.Method.invoke(Method.java:498)       在sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)       在sun.rmi.transport.Transport $ 1.run(Transport.java:200)       在sun.rmi.transport.Transport $ 1.run(Transport.java:197)       在java.security.AccessController.doPrivileged(本机方法)       在sun.rmi.transport.Transport.serviceCall(Transport.java:196)       在sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)       在sun.rmi.transport.tcp.TCPTransport $ ConnectionHandler.run0(TCPTransport.java:834)       在sun.rmi.transport.tcp.TCPTransport $ ConnectionHandler.lambda $ run $ 0(TCPTransport.java:688)       在java.security.AccessController.doPrivileged(本机方法)       在sun.rmi.transport.tcp.TCPTransport $ ConnectionHandler.run(TCPTransport.java:687)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)       在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)       在java.lang.Thread.run(Thread.java:748)

这是我的AbstractBinder用于DI的实现:

public class UsersDependencyBinder extends AbstractBinder {    
    private static final Logger LOG = LoggerFactory.getLogger(UsersDependencyBinder.class);

    @Override
    protected void configure() {
        try {
            // Acts as a Singleton
            bind(new UsersDAO(new CachedDbConnector()));
        } catch (ClassNotFoundException e) {
            LOG.error("Error registering the DB Driver Manager", e);
        }
    }
}

这是我注射的方式:

private final UsersDAO dao;

@Inject
public UserResource(UsersDAO dao) {
    this.dao = dao;
}

其余代码之前工作正常,只是一个ping,它什么也不做,我试图使基础结构正常工作(服务器响应API调用,数据库已启动并正在运行等)。在开始编写代码之前。

我尝试添加:

implementation ("org.glassfish.jersey.bundles:jaxrs-ri:2.27")

但是它也不起作用。

1 个答案:

答案 0 :(得分:0)

答案实际上很简单,但是没有充分的记录。

我的Tomcat在Windows本地运行,并随Windows安装程序一起安装,它将位于Program File文件夹中,该文件夹具有普通用户的写保护,因此IntelliJ IDEA无法部署我的war文件放在webapps文件夹中。

有两种解决方法:

  1. 更改webapp文件夹权限以允许来自用户的写入。
  2. 在Tomcat安装中的webapp内更改默认的server.xml文件夹位置。