Jersey Web应用程序的Docker映像 [英] Docker Image of Jersey Web Application

查看:130
本文介绍了Jersey Web应用程序的Docker映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从在Tomcat服务器上运行的Jersey网络应用程序创建Docker映像.我正在Windows 7计算机上进行开发.

I'm trying to create a Docker image from my Jersey web application running on a Tomcat server. I'm developing on a Windows 7 machine.

我已将Web应用程序部署在开发机器上的本地Tomcat 8.0.14应用程序服务器上,并且一切正常.

I have deployed the web application on a local Tomcat 8.0.14 application server on my development machine and everything works as expected.

要创建Docker映像,我将以下Dockerfile.放在与my-web-app.war文件相同的目录中.

To create the Docker image I put the following Dockerfile. in the same directory as the my-web-app.war file.

FROM tomcat:8.0-jre8
ADD /my-web-app.war /usr/local/tomcat/webapps/
CMD ["catalina.sh", "run"]

然后,我使用以下命令创建图像:

After that I'm creating the image with the following command:

docker build -t my-web-app .

此操作已成功完成,并且使用docker images -a命令显示了图像.

This is done sucessfully and the images shows up with the docker images -a command.

此后,我通过此图像开始:

After that I start the image through this:

winpty docker run --rm -it -p 8080:8080 my-web-app

命令提示符显示服务器已成功启动,当我尝试访问Web应用程序时,它也起作用:

The command prompt show that the server is successfully started and when I tried to access the web application this also works:

http://192.168.99.100:8080/my-web-app

显示相应的HTML欢迎页面.

shows the appropriate HTML welcome page.

当我尝试访问任何实际的Jersey RESTful Web服务时,就会出现此问题.每当我尝试访问与HTML页面不同的内容时,都会收到以下错误消息:

The issue arises when I try to access any of the actual Jersey RESTful web services. Any time I try to access something different than an HTML page I get the following error message:

javax.servlet.ServletException: Servlet.init() for servlet My Web Application threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2527)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2516)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:748)
root cause

java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;
    org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:308)
    org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:337)
    org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:178)
    org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:370)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2527)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2516)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:748)

我认为创建WAR文件可能有问题,但如果这样,如何将应用程序成功部署到我的本地Tomcat服务器上.

I assume there might be something wrong with the creation of the WAR file but if so how can the application be successfully deployed on my local Tomcat server.

如果对此感兴趣,这里是我用来构建WAR的pom.xml文件:

If this is of any interest here is the pom.xml file I used to build the WAR:

<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>my.webapp.host</groupId>
    <artifactId>my-web-app</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>my-web-app</name>

    <build>
        <finalName>my-web-app</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.26-b03</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>2.26-b03</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>2.26-b03</version>
        </dependency>

        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>4.0.0-b05</version>
        </dependency>   

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.19.3</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.19.3</version>
        </dependency>

        <!-- persistence api -->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.6.4</version>
        </dependency>

        <!-- additional apis -->        
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

         <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>

        <!-- own apis -->  
        <dependency>
            <groupId>my.own.utility.api</groupId>
            <artifactId>utility-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

有人知道这里的问题吗?

Does anyone know what the issue here is?

感谢您的帮助.

问候

推荐答案

这是对所有遇到NoSuchMethodError的人的深入概述和解决方案,尤其是在球衣上下文中.

Here is an indepth overview and a solution to all who encounter NoSuchMethodError especially in jersey context.

问题是

java.lang.NoSuchMethodError:javax.ws.rs.core.Application.getProperties()Ljava/util/Map;

java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;

在Java中,当JVM无法找到在指定类中指定的方法时,抛出NoSuchMethodError. 来自 https://docs.oracle.com/javase /9/docs/api/java/lang/NoSuchMethodError.html

In Java NoSuchMethodError is thrown when the JVM cant find the method specified in the specified class. From https://docs.oracle.com/javase/9/docs/api/java/lang/NoSuchMethodError.html

如果应用程序尝试调用类的指定方法(静态或实例),并且该类不再具有该方法的定义,则抛出该错误.

Thrown if an application tries to call a specified method of a class (either static or instance), and that class no longer has a definition of that method.

在您的情况下,JVM抱怨javax.ws.rs.core.Application没有getProperties()方法.

In your case JVM is complaining that javax.ws.rs.core.Application doesnot have getProperties() method.

Jesey 2.x使用JEE 7 .在JEE 7版本的javax.ws.rs.core.Application具有

Jesey 2.x uses JEE 7. In JEE 7 version of javax.ws.rs.core.Application has

  • getClasses()
  • getSingeltons()
  • getProperties() methods defined. https://docs.oracle.com/javaee/7/api/javax/ws/rs/core/Application.html

Jesey 1.x使用JEE 6 .在JEE 6版本中,javax.ws.rs.core.Application仅具有

Jesey 1.x uses JEE 6. In JEE 6 version of javax.ws.rs.core.Application only has

  • getClasses()
  • getclass()
  • getClasses()
  • getclass()

但是未定义getProperties(). https://jersey.github. io/apidocs/1.19.1/jersey/javax/ws/rs/core/Application.html

解决方案

您在pom.xml中定义了jersey 2.x和1.x版本.因此,您的类路径中同时存在JEE6和javax.ws.rs.core.Application的7个版本,并且类加载器将加载尚未定义getProperties()但您的应用程序仍然希望执行getProperties()的appliction类的JEE 6版本.因此是错误.

You have jersey 2.x and 1.x versions defined in your pom.xml. Therefore there are both JEE6 and 7 versions of javax.ws.rs.core.Application in your classpath and classloader loads JEE 6 version of appliction class which doesnot have getProperties() defined but yor application wants to execute getProperties() anyway. Hence the error.

从pom.xml中删除所有jersey 1.x版本,并保留到jersey 2.x版本.类加载器将负责其余的工作.

Remove all jersey 1.x versions from your pom.xml and stick to jersey 2.x version. The classloader will take care of the rest.

这篇关于Jersey Web应用程序的Docker映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆