gpt4 book ai didi

java - 在 tomcat docker 上部署 java REST 服务

转载 作者:行者123 更新时间:2023-11-28 22:46:33 25 4
gpt4 key购买 nike

我正在尝试将一个简单的 REST 服务部署到在 docker 上运行的 tomcat,但我无法连接到我尝试部署的服务。

我创建的简单服务在这个类中:

@Path("/test")
public class RestTest {

public static final String JSON_RPC = "2.0";

@GET
@Path("/hello")
@Produces(MediaType.APPLICATION_JSON)
public Response processHelloRequestGet() {
String answer = "Hello There!";
JsonRpcResponse rpcResponse = new JsonRpcResponse();
rpcResponse.setId("42");
rpcResponse.setJsonRpc(JSON_RPC);
rpcResponse.setResult(answer);
return Response.status(Status.OK).entity(rpcResponse).build();
}
}

它只是以 JSON-RPC 响应的形式创建一个简单的静态答案。

我的 web.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<display-name>REST Test</display-name>

<servlet>
<servlet-name>jerseyServlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>net.jfabricationgames.rest_test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jerseyServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

我的 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>net.jfabricationgames.rest_test</groupId>
<artifactId>RestTest</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>RestTest Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<!-- JAX-RS for REST -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1-m07</version><!-- <version>2.0</version> -->
</dependency>

<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>2.26</version><!-- <version>2.10.1</version> -->
</dependency>

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

<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.26</version><!-- <version>2.10.1</version> -->
</dependency>

<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.26</version>
</dependency>

<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>0.99</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/asm/asm -->
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-bundle -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-server -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-core -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19.2</version>
</dependency>

</dependencies>
<build>
<finalName>RestTest</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/rest_test</path>
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>
</project>

现在,如果我在 tomcat 服务器上运行项目从 eclipse 中,一切都按预期工作(使用运行配置 Base directory: ${workspace_loc:/RestTest}Goals: tomcat7:run),我可以使用 URL http://localhost:8080/rest_test/rest/test/hello 访问该服务。

问题现在是我希望我的服务在服务器上的 docker 内的 tomcat 上运行,这似乎无法正常工作。

到目前为止我尝试过的是将项目导出到 .war 文件(使用 maven(gloals:clean install compile)或 eclipse 导出功能),将它们放在我构建的目录中docker 文件,创建 docker 文件并运行它。

我正在使用的 Dockerfile(在 this tutorial 之后)看起来像这样:

FROM tomcat:8.0-alpine
LABEL maintainer="deepak@softwareyoga.com"

ADD rest_test.war /usr/local/tomcat/webapps/

EXPOSE 8080
CMD ["catalina.sh", "run"]

当使用命令 docker run -p 80:8080 mywebapp 运行 docker 容器时,我得到以下输出(这对我来说似乎是正确的):

docker run -p 80:8080 mywebapp
21-Oct-2019 16:49:55.404 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.0.53
21-Oct-2019 16:49:55.407 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jun 29 2018 14:42:45 UTC
21-Oct-2019 16:49:55.408 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.0.53.0
21-Oct-2019 16:49:55.408 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
21-Oct-2019 16:49:55.408 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 5.0.0-31-generic
21-Oct-2019 16:49:55.409 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
21-Oct-2019 16:49:55.409 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/lib/jvm/java-1.7-openjdk/jre
21-Oct-2019 16:49:55.409 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.7.0_181-b01
21-Oct-2019 16:49:55.410 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
21-Oct-2019 16:49:55.410 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat
21-Oct-2019 16:49:55.411 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat
21-Oct-2019 16:49:55.411 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
21-Oct-2019 16:49:55.412 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
21-Oct-2019 16:49:55.412 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
21-Oct-2019 16:49:55.412 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
21-Oct-2019 16:49:55.413 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
21-Oct-2019 16:49:55.413 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
21-Oct-2019 16:49:55.413 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
21-Oct-2019 16:49:55.414 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
21-Oct-2019 16:49:55.414 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.2.17 using APR version 1.6.3.
21-Oct-2019 16:49:55.414 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
21-Oct-2019 16:49:55.418 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.0.2o 27 Mar 2018)
21-Oct-2019 16:49:55.528 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"]
21-Oct-2019 16:49:55.556 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"]
21-Oct-2019 16:49:55.561 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1095 ms
21-Oct-2019 16:49:55.677 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
21-Oct-2019 16:49:55.677 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.53
21-Oct-2019 16:49:55.760 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /usr/local/tomcat/webapps/rest_test.war
21-Oct-2019 16:49:56.810 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /usr/local/tomcat/webapps/rest_test.war has finished in 1,050 ms
21-Oct-2019 16:49:56.820 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /usr/local/tomcat/webapps/sample.war
21-Oct-2019 16:49:56.911 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /usr/local/tomcat/webapps/sample.war has finished in 91 ms
21-Oct-2019 16:49:56.913 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/docs
21-Oct-2019 16:49:56.986 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/docs has finished in 73 ms
21-Oct-2019 16:49:56.992 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/examples
21-Oct-2019 16:49:57.799 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/examples has finished in 807 ms
21-Oct-2019 16:49:57.801 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/ROOT
21-Oct-2019 16:49:57.882 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/ROOT has finished in 82 ms
21-Oct-2019 16:49:57.885 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/manager
21-Oct-2019 16:49:57.984 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/manager has finished in 99 ms
21-Oct-2019 16:49:57.993 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/host-manager
21-Oct-2019 16:49:58.081 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/host-manager has finished in 88 ms
21-Oct-2019 16:49:58.088 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"]
21-Oct-2019 16:49:58.156 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-apr-8009"]
21-Oct-2019 16:49:58.187 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 2625 ms

目前有效是 docker 容器似乎已正确启动,并且我可以通过在浏览器中输入 localhost 来访问 tomcat“主页”(请参阅下图)。另外,我的 .war 文件似乎已正确添加和提取(在 webapps 目录中有一个 rest_test.war 文件和一个 rest_test 目录tomcat docker 容器)。此外,从 eclipse 中启动它时一切正常。

Tomcat 'homepage' is reachable

什么还没有工作是到达我创建和部署的其余服务。我尝试使用 url localhost/rest_test/rest/test/hello 访问它,但我收到 HTTP 错误 404 - Not Found。

有点奇怪的是,当调用 url localhost/rest_test/rest/... 时,我没有在 HTTP 错误 404 中收到消息,但是我在调用任何其他 url 时收到一条消息(如下图所示),这让我认为我正在尝试的并不是完全错误的。

The 'assumed correct' url doesn't contain a message in the HTTP error 404

A wrong URL seems to contain a message in the http 404 error

提前致谢。

编辑:使用 docker 镜像 tomcat:9.0-alpine 更新到 tomcat 9,但这似乎没有任何改变。 .war 文件的内容如下所示:

enter image description here

最佳答案

war 文件似乎没有正确构建。 WEB-INF/libs缺少包含 jar 应用程序依赖项的目录。关于 Web 应用程序归档文件的目录布局的更多信息 here .

启动期间抛出的异常记录到/usr/local/tomcat/logs/localhost.<timestamp>.log .这就是一切似乎正常工作的原因。 localhost.log应该包含错误:

java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

如评论中所述,jersey 2.26 无法与 java 1.7.0_181 一起运行。由于在应用程序启动期间不会加载库,因此输出中没有 Java 版本不兼容错误。

现在,假设您使用 tomcat 9 和 java 8 运行,第一步是修复构建。我建议从命令行运行 maven。你的pom.xml看起来很好,应该会引发一场有效的 war 。

下一步是设置 javax.ws.rs-api 的范围图书馆提供。 jersey-bundle库的版本略有不同 javax.ws.rs类,如果同时保留这两个类,则可能会遇到类加载问题。

 <dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>

这会解决问题,但配置仍然存在问题,因为您将 jersey 1.x 与 jersey 2.x 库混合使用。此外 jersey-bundle不应与 Maven 一起使用。 该包仅适用于不使用 Maven 依赖系统的开发人员。

使用 jersey 2 库的基本依赖配置应该如下所示:

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

<jersey2.version>2.26</jersey2.version>
<jaxrs.version>2.1.1</jaxrs.version>
</properties>
<dependencies>
<!-- JAX-RS -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${jaxrs.version}</version>
</dependency>
<!-- Jersey -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey2.version}</version>
</dependency>
</dependencies>

web.xml也应该修改:

<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>net.jfabricationgames.rest_test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

希望对您有所帮助。

关于java - 在 tomcat docker 上部署 java REST 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58491292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com