Tomcat找不到Web服务404

问题描述 投票:1回答:1

我正在创建一个简单的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>

这是错误快照enter image description here

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

java web-services jax-rs tomcat9
1个回答
0
投票
您需要在jax-rs应用程序中添加@ApplicationPath

来自Oracle Docs

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

例如:在项目中添加以WorldInformationApp注释的类@ApplicationPath

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

尝试使用此URL访问资源:http://localhost:8080/worldinfo/worldinformation/getCountries    
© www.soinside.com 2019 - 2024. All rights reserved.