【问题标题】:Tomcat Couldn't find Webservice 404Tomcat 找不到 Web 服务 404
【发布时间】:2020-09-25 09:16:53
【问题描述】:

我正在创建一个简单的 Web 服务项目,该项目从国家/地区的数据库中提取数据并以 JSON 形式显示,我的项目没有抛出任何错误并成功构建,但无法找到已部署的资源。

WorldInformation.java

package com.webservices;

import java.util.List;

import com.webservices.models.Country;
import com.webservices.services.WorldInformationService;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/worldinformation")
public class WorldInformation {

    WorldInformationService  worldInformationService = new WorldInformationService();

    @GET
    @Path("/getCountries")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Country> getCountries(){
        System.out.println("reached point 1");
        List<Country> countryList = worldInformationService.getCountries();
        return countryList;
    }

    @POST
    @Path("/setCountry/{country}/{countryCode}")
    @Consumes(MediaType.TEXT_PLAIN)
    public void setCountry(@PathParam("country") String country,@PathParam("countryCode") String countryCode){
        worldInformationService.setCountry(country,countryCode);
    }

}

这是web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</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.webservices</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/*</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>com.webservices</groupId>
    <artifactId>WorldInformation</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>WorldInformation</name>


    <build>
        <finalName>WorldInformation</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>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
        </dependency>
        <!-- uncomment this to get JSON support
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-binding</artifactId>
        </dependency>
        -->
          <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.10.Final</version>
</dependency>
  <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.19</version>
</dependency>
    </dependencies>
    <properties>
        <jersey.version>3.0.0-M1</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

这是错误快照

我似乎无法弄清楚如何解决这个问题以及出了什么问题,非常感谢您对此提供的帮助。

【问题讨论】:

  • 您缺少上下文路径,请尝试 localhost:8080/WorldInformation/worldinformation/getCountries

标签: java web-services jax-rs tomcat9


【解决方案1】:

我认为您还需要在您的 jax-rs 应用程序中添加 @ApplicationPath

注意:该 URL 也缺少上下文路径。

例如:http://localhost:8080/&lt;context-path&gt;/servlet/path

来自Oracle Docs

@ApplicationPath 注解用于定义 URL 映射 对于整个应用程序。 @ApplicationPath 指定的路径是 由@Path 注释指定的所有资源 URI 的基本 URI 资源类。

例如:在项目中添加一个WorldInformationApp注解@ApplicationPath的类。

@ApplicationPath("/worldinfo")
public class WorldInformationApp extends Application {

}

尝试使用此 url 访问资源:http://localhost:8080/&lt;context-path&gt;/worldinfo/worldinformation/getCountries

注意:我认为您为 jax-rs 应用程序使用了错误的库。

例如:应使用javax.ws.rs.* 而不是jakarta.ws.rs.*

【讨论】:

  • 您好,感谢您的建议,但它不起作用。我尝试关注 url 'localhost:8080/WorldInformation/worldinfo/worldinformation/…' 和 'localhost:8080/worldinfo/worldinformation/getCountries' 都给出了未找到的错误。
  • @Sid111Math 你有没有按照我说的在你的项目中添加WorldInformationApp这个类?
  • 是的,我添加了那个文件,但结果还是一样
  • @Sid111Math 但是怎么做呢?您可以添加代码示例进行测试吗?我会解决的。
  • @Sid111Math 您为 jax-rs 应用程序使用了错误的库?例如:应该使用 javax.ws.rs.GET 而不是 jakarta.ws.rs.GET
【解决方案2】:

感谢您的宝贵反馈,这对我有用。正如有人建议的那样。我没有看正确的观点。我在尝试连接的 url 中缺少应用程序名称。虽然我遇到了其他问题,但我设法解决了它们,现在它对我有用。

localhost:8080/WorldInformation/worldinformation/getCountries

感谢您对此提供的宝贵帮助。

【讨论】:

    猜你喜欢
    • 2016-03-17
    • 2018-07-13
    • 2013-07-16
    • 2018-02-04
    • 1970-01-01
    • 2016-10-24
    • 2020-07-31
    • 2016-02-28
    • 1970-01-01
    相关资源
    最近更新 更多