首页 > 解决方案 > Jax rs 资源 404 未找到错误

问题描述

使用 jax-rs 在 java 中运行应用程序时出现以下错误

HTTP Status 404 – Not Found
Type Status Report 
Message /ajavaeeconcurrency/resources/greetUser
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/9.0.30

我正在使用 java8 并尝试使用 jax-rs 创建restAPI 下面是代码

package com.app.rest;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("resources")

public class JAXRSConfiguration extends Application {

}

下面是资源类

package com.app.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/greetUser")
public class GreetResource {
    
    @GET
    public String greetUser()
    {
        return "java EE concurrency starts.";
    }
    

这是我的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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>ajavaeeconcurrency</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.eclipse.microprofile</groupId>
            <artifactId>microprofile</artifactId>
            <version>2.0.1</version>
            <type>pom</type>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    
    <build>
        <finalName>ajavaeeconcurrency</finalName>
    </build>
    
    <properties>
    
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>
</project>

artifactId的是ajavaeeconcurrency。我正在尝试使用以下访问资源URI

GET http://localhost:8080/ajavaeeconcurrency/resources/greetUser

我收到404错误。我该如何解决相同的问题

标签: javamavenjax-rs

解决方案


我认为您的 URL 是 http://localhost:8080/resources/greetUser。

如果需要,通常在应用程序服务器中配置带有 artifactId 部分的 URL。


推荐阅读