jersey - 尝试使用 Jersey 3 和 Tomcat 10 访问端点时出现 404
问题描述
几天来一直在尝试解决这个问题。只是尝试在 Maven 中使用 Jersey 3 和 Tomcat 10 重新创建一个简单的“Hello World”REST api。创建项目的 WAR 文件后,我可以访问 index.jsp(在我创建项目时默认创建),但是当我尝试访问“/helloworld”端点时出现错误 404。这是我的代码:
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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>web-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp</groupId>
<artifactId>jakarta.servlet.jsp-api</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.el</groupId>
<artifactId>jakarta.el-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.websocket</groupId>
<artifactId>jakarta.websocket-api</artifactId>
<version>2.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.security.enterprise</groupId>
<artifactId>jakarta.security.enterprise-api</artifactId>
<version>2.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-web-api</artifactId>
<version>9.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>web-app</finalName>
</build>
</project>
你好世界资源
/*
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package jersey;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
/**
*
* @author Jakub Podlesak
*/
@Path("helloworld")
public class HelloWorldResource {
public static final String CLICHED_MESSAGE = "Hello World!";
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getHello() {
return CLICHED_MESSAGE;
}
}
JaxRsApplication
/*
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package jersey;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;
/**
* JAX-RS Application class for this example.
*
* @author Martin Matula
*/
@ApplicationPath("/api")
public class JaxRsApplication extends Application {
private final Set<Class<?>> classes;
public JaxRsApplication() {
HashSet<Class<?>> c = new HashSet<Class<?>>();
c.add(HelloWorldResource.class);
classes = Collections.unmodifiableSet(c);
}
@Override
public Set<Class<?>> getClasses() {
return classes;
}
}
错误 图片
使用 jdk 11,我尝试使用 Tomcat 10 在 Docker 中部署,并在本地直接将 Tomcat 服务器下载到我的 PC 中。
建议?
解决方案
Jersey 需要适当的容器模块来部署 REST 应用程序。您添加了jersey-container-jdk-http
,它与 JDK Http Server 一起使用(参见文档)。
相反,您需要的是在每个 Servlet 3.x 环境中工作的jersey-container-servlet
模块(参见文档)。因此,您需要添加此依赖项:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>3.0.2</version>
</dependency>
备注:Tomcat 10 不是完整的 Jakarta EE 9 服务器,因此您还应该删除jakarta.jakartaee-web-api
依赖项。
推荐阅读
- java - 对 Cloud Firestore 社交媒体结构使用“array-contains”查询
- google-cloud-platform - 如何检查 gcloud 后端服务/url 映射是否准备好
- sql-server - MS Access 查询中的“无限”是什么意思
- xslt - XSLT 需要计算父节点内 count 大于 1 的节点数
- javascript - 无法在带有 webpack 的 Laravel 5.7 刀片模板中使用带有一些 jQuery 的旧 JS 函数 - 找不到方法
- python - Python:循环属性并就地修改它们
- android - 滚动 ScrollView 时如何更改 TabLayout 上的选项卡?
- android - 制作自定义纵横比的应用程序(无论正常比例如何)
- c# - 在 C# 中,如何访问在运行时创建的一组控件标签的元素?
- typescript - TypeScript:如何在内部属性上使用 Reflect.metadata?