Resteasy 应用程序总是返回 404

Posted

技术标签:

【中文标题】Resteasy 应用程序总是返回 404【英文标题】:Resteasy application always returning 404s 【发布时间】:2021-01-06 11:59:28 【问题描述】:

我启动了一个只有这两个类的应用程序作为健全性测试,但我只得到 404。我在 localhost:8080/TestApi-1.0-SNAPSHOT/test 上运行来自 Postman 的 get 请求,假设它应该返回“Hello World”,但我得到了 404 响应。我使用 JBoss/Wildfly 14.0.0-Final 作为服务器供参考。任何帮助,将不胜感激。我将包括两个类,从 IntelliJ 部署服务器时的控制台输出和 POM。

<------------------- TestEntry.java ------------------------>
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/")
public class HealthyLivingApiEntry extends Application 

<------------------- TestResource.java ------------------------>
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

public class HelloResource 

    @Path("/test")
    @GET
    public Response hello()
        System.out.println("Running test service here");
        return Response.ok("Hello World").build();
    

<?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>TestApi</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>TestApi</name>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <junit.version>5.6.2</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.enterprise.concurrent</groupId>
            <artifactId>javax.enterprise.concurrent-api</artifactId>
            <version>1.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.1.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>$junit.version</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>$junit.version</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.0</version>
            </plugin>
        </plugins>
    </build>
</project>

20:08:41,174 INFO  [org.infinispan.factories.GlobalComponentRegistry] (MSC service thread 1-1) ISPN000128: Infinispan version: Infinispan 'Estrella Galicia' 9.3.1.Final
20:08:41,411 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 2) WFLYCLINF0002: Started client-mappings cache from ejb container
20:08:41,862 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 2) RESTEASY002225: Deploying javax.ws.rs.core.Application: class TestEntry
20:08:41,865 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 2) WFLYUT0021: Registered web context: '/TestApi-1.0-SNAPSHOT' for server 'default-server'
20:08:41,886 INFO  [org.jboss.as.server] (management-handler-thread - 1) WFLYSRV0010: Deployed "TestApi-1.0-SNAPSHOT.war" (runtime-name : "TestApi-1.0-SNAPSHOT.war")
[2020-09-19 08:08:41,900] Artifact TestApi:war: Artifact is deployed successfully
[2020-09-19 08:08:41,900] Artifact TestApi:war: Deploy took 1,754 milliseconds

【问题讨论】:

【参考方案1】:

将 TestResource.java 更改为如下所示有帮助。看起来由于某种原因,一个类未被识别为带有 @Path 注释的资源。

<------------------- TestResource.java ------------------------>
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("/")
public class HelloResource 

    @Path("/test")
    @GET
    public Response hello()
        System.out.println("Running test service here");
        return Response.ok("Hello World").build();
    

【讨论】:

以上是关于Resteasy 应用程序总是返回 404的主要内容,如果未能解决你的问题,请参考以下文章

JAX-RS (Resteasy 3.5.0.Final) + Wildfly 12 + Java 9 + maven = 404 未找到,但 JAX-RS (Resteasy 3.5.0.Final

Laravel 路由上的 PHPunit 测试总是返回 404

C# HttpClient.SendAsync 总是返回 404 但 URL 在浏览器中有效

Wildfly14 的 Resteasy:并非所有字段都返回

NancyFX:带有查询字符串参数的路由总是返回 404 NotFound

PHPUnit / Lumen 总是返回 404