我试过这个球衣你好世界教程:https://www.codeproject.com/Articles/1060073/RESTFul-Web-Service-Using-Jersey-x-Part-I 我有一个欢迎文件index.html和一个rest url / rest / message / helloworld
我可以在tomcat 9中成功部署战争,一切正常。
我在wildfly 10上部署了相同的war文件,但是其他url无效:
1)我在管理面板中看到它成功部署。
2)我可以访问欢迎文件index.html,它显示简单的问候
但是我无法访问其中显示Not Found的其他网址...同样适用于Tomcat。 Wildfly是否需要为Jersey基地休息一些配置?
我正在使用Jersey 2.6
答案 0 :(得分:3)
Wildfly不需要Jersey--它已经包含了JAX-RS实现。如果我可以为JEE服务器推荐一个更简单的解决方案...
您需要3个文件:
RestApplicationConfig.java
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
/**
* Used to bootstrap JAX-RS. Otherwise this class is
* not directly used.
*
*/
@ApplicationPath("/rest")
public class RestApplicationConfig extends Application {
// intentionally empty
}
HeartbeatService.java
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
/**
* Provides a simple heartbeat.
*
*/
@Path("/v1/heartbeat")
public class HeartbeatService {
/**
* Get the heartbeat. Basically if you can hit this "service"
* then the machine and process are up.
*
* @return a HTTP 200 with a simple "OK" text response packet.
*
*/
@Produces({ MediaType.TEXT_PLAIN })
@GET
public Response getHeartBeat() {
return Response.ok("OK").build();
}
}
pom.xml
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>jaxrs-simple-sample</artifactId>
<groupId>com.hotjoe.jaxrs</groupId>
<name>JAXRS Simple Sample</name>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/jaxrs-api -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<version>3.0.12.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
(取自我的github example)
构建完成后,您将在http://localhost:8080/jaxrs-simple-sample-1.0-SNAPSHOT/rest/v1/heartbeat
访问REST服务如果您更喜欢示例中显示的服务可以替换上面的简单服务。
一个重要的注意事项 - 这是一个JavaEE项目 - 它将不在Tomcat下运行,因为它利用了完整JavaEE服务器中包含的库和工具。 Tomcat实现了JavaEE规范(以及其他一些部分)的servlet / jsp部分,但不实现JAX-RS部分。但是,Glassfish的运行几乎没有变化。唯一的变化是在pom.xml中。