我需要什么才能使这个极简 REST 示例正常工作?
项目名称:hello-rest
应用程序代码:
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("resources")
public class MyJAXWSApp extends Application {
}
消息资源:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("message")
public class MessageResource {
@GET
public String Hello() {
return "hello!";
}
}
MAVEN 配置:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
问题:
到目前为止,当我调用 Service URI 时,在 WildFly 10x 中通过 Eclipse Neon 进行部署会导致 404 错误:
http://localhost:8080/hello-rest/resources/message
来源:Adam Bien
PS:服务器部署正常:
22:20:04,486 INFO [org.wildfly.extension.undertow] (MSC service thread 1-2) WFLYUT0006: Undertow HTTP listener default listening on 127.0.0.1:8080 ...
22:20:04,582 INFO [org.jboss.as.server.deployment] (MSC service thread 1-5) WFLYSRV0027: Starting deployment of "hello-rest-0.0.1-SNAPSHOT.war" (runtime-name: "hello-rest-0.0.1-SNAPSHOT.war") ...
22:20:04,974 INFO [org.wildfly.extension.undertow] (MSC service thread 1-3) WFLYUT0006: Undertow HTTPS listener https listening on 127.0.0.1:8443 ....
22:20:06,992 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 59) RESTEASY002225: Deploying javax.ws.rs.core.Application: class JAXRSConfiguration ...
22:20:07,074 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 59) WFLYUT0021: Registered web context: /hello-rest-0.0.1-SNAPSHOT
22:20:07,135 INFO [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "hello-rest-0.0.1-SNAPSHOT.war" (runtime-name : "hello-rest-0.0.1-SNAPSHOT.war")
最佳答案
我在服务器输出中看到的一件事是将路径注册为“/hello-rest-0.0.1-SNAPSHOT”。因此,在您更改 Maven 配置以不将版本附加到您的 war 之前,您必须在调用服务时将“-0.0.1-SNAPSHOT”添加到您的 URL 中。
即<a href="http://localhost:8080/hello-rest-0.0.1-SNAPSHOT/resources/message" rel="noreferrer noopener nofollow">http://localhost:8080/hello-rest-0.0.1-SNAPSHOT/resources/message</a>
我也总是看到资源(而不是方法)上的 ApplicationPath 和 Path 注释以“/”开头。我不确定它是否是必需的,但无论如何我都会推荐它作为最佳实践。
更新:查了一下here从 JAX-RS 2 开始,不需要尾部和前导斜杠。
关于java - 带有路径注释的简单 REST 服务 - 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42962595/