【问题标题】:HTTP 404 with Java + Maven + Jersey2 + Tomcat 8.5 and IntelliJ IDEAHTTP 404 与 Java + Maven + Jersey2 + Tomcat 8.5 和 IntelliJ IDEA
【发布时间】:2017-03-14 13:58:51
【问题描述】:

我正在尝试使用 IntelliJ IDEA、Jersey v2.25.1、Maven 和 Tomcat v8.5.11 在 Java (JDK 1.8.0.121) 中构建一个简单的 REST 服务。

我在 com.example 包中编写了一个带有 GET 方法的简单类

package com.example;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class HelloWorld {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getMessage() {
        return "Hello world!";
    }
}

我的 web.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<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">

<display-name>Simple Web Application</display-name>

<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>com.example</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

我的 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>com.example</groupId>
<artifactId>simple_rest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <jersey2.version>2.25.1</jersey2.version>
    <jaxrs.version>2.0.1</jaxrs.version>
</properties>

<repositories>
    <repository>
        <id>maven2-repository.java.net</id>
        <name>Java.net Repository for Maven</name>
        <url>http://download.java.net/maven/2/</url>
        <layout>default</layout>
    </repository>
</repositories>

<dependencies>
    <!-- JAX-RS -->
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>${jaxrs.version}</version>
    </dependency>
    <!-- Jersey 2.25.1 -->
    <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>
</dependencies>

我尝试在我的第一台电脑上运行,它可以工作;但如果我在笔记本电脑上运行相同的代码,当我访问 http://localhost:8080/hello 时,它会返回 HTTP 404 错误。

当我收到 404 错误时,我在服务器控制台上没有收到任何编译错误和错误消息。

您知道缺少什么吗? 在此先感谢:)

【问题讨论】:

  • 确保将其部署在正确的 webapp 下?此外,重新启动 tomcat 可能会有所帮助,但我想你已经尝试过了
  • 我已经将应用部署在tomcat文件夹的webapp文件夹下。我也尝试重新启动Tomcat,但问题仍然存在。 :(

标签: java rest maven tomcat jersey


【解决方案1】:

在其他班级添加@ApplicationPath怎么样?这是示例代码。

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/rest-v1")
public class YourApplication extends Application {}

之后,也许localhost:8080/rest-v1/hello就可以访问了。

【讨论】:

  • 不错!但是为什么应用程序在不同的服务器上运行呢?
猜你喜欢
  • 1970-01-01
  • 2016-01-05
  • 1970-01-01
  • 2013-01-06
  • 2016-02-09
  • 2017-09-10
  • 1970-01-01
  • 2015-01-15
  • 2021-02-21
相关资源
最近更新 更多