Resteasy应用始终返回404s。

huangapple go评论90阅读模式
英文:

Resteasy application always returning 404s

问题

    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;
    
    @ApplicationPath("/")
    public class HealthyLivingApiEntry extends Application {
    }
    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
英文:

I started an application with only these two classes as a sanity test, but I only get 404s. I run a get request from Postman at localhost:8080/TestApi-1.0-SNAPSHOT/test, which should hypothetically return "Hello World" but I get a 404 response. I'm using JBoss/Wildfly 14.0.0-Final as the server for reference. Any help would be appreciated. I'll include the two classes, the console output when deploying the server from IntelliJ, and the POM.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&lt;------------------- TestEntry.java ------------------------&gt;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath(&quot;/&quot;)
public class HealthyLivingApiEntry extends Application {
}

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&lt;------------------- TestResource.java ------------------------&gt;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
public class HelloResource {
@Path(&quot;/test&quot;)
@GET
public Response hello(){
System.out.println(&quot;Running test service here&quot;);
return Response.ok(&quot;Hello World&quot;).build();
}
}

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;com.example&lt;/groupId&gt;
&lt;artifactId&gt;TestApi&lt;/artifactId&gt;
&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
&lt;name&gt;TestApi&lt;/name&gt;
&lt;packaging&gt;war&lt;/packaging&gt;
&lt;properties&gt;
&lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
&lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
&lt;junit.version&gt;5.6.2&lt;/junit.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;javax.enterprise.concurrent&lt;/groupId&gt;
&lt;artifactId&gt;javax.enterprise.concurrent-api&lt;/artifactId&gt;
&lt;version&gt;1.1&lt;/version&gt;
&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;javax.ws.rs&lt;/groupId&gt;
&lt;artifactId&gt;javax.ws.rs-api&lt;/artifactId&gt;
&lt;version&gt;2.1.1&lt;/version&gt;
&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;javax.servlet&lt;/groupId&gt;
&lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
&lt;version&gt;4.0.1&lt;/version&gt;
&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
&lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
&lt;version&gt;${junit.version}&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
&lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
&lt;version&gt;${junit.version}&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
&lt;version&gt;3.3.0&lt;/version&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

20:08:41,174 INFO  [org.infinispan.factories.GlobalComponentRegistry] (MSC service thread 1-1) ISPN000128: Infinispan version: Infinispan &#39;Estrella Galicia&#39; 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: &#39;/TestApi-1.0-SNAPSHOT&#39; for server &#39;default-server&#39;
20:08:41,886 INFO  [org.jboss.as.server] (management-handler-thread - 1) WFLYSRV0010: Deployed &quot;TestApi-1.0-SNAPSHOT.war&quot; (runtime-name : &quot;TestApi-1.0-SNAPSHOT.war&quot;)
[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

<!-- end snippet -->

答案1

得分: 0

以下是翻译后的内容:

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

<-- 开始代码片段 -->

<-- 语言: lang-js -->

    <------------------- 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();
        }

    }

<-- 结束代码片段 -->
英文:

Changing TestResource.java to look like below helped. Looks like for some reason a class isn't recognized as a resource with the @Path annotation.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&lt;------------------- TestResource.java ------------------------&gt;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
@Path(&quot;/&quot;)
public class HelloResource {
@Path(&quot;/test&quot;)
@GET
public Response hello(){
System.out.println(&quot;Running test service here&quot;);
return Response.ok(&quot;Hello World&quot;).build();
}
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年9月20日 08:25:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63974502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定