空web.xml
就行。
只需在您的类路径中添加一些简单的依赖项即可。例如,如果您使用 maven,您可以将其添加到您的pom.xml
:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.1.GA</version>
<scope>provided</scope> <!-- provided if youre deploying to jboss as 7.1 -->
</dependency>
然后仅使用此类设置应用程序:
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/rest")
public class JaxRsActivator extends Application {
}
只是为了确保,添加这样的资源:
@Path("/hello")
public class HelloResource {
@GET
@Produces("text/plain")
public String helloResource() {
return "Hello! It's "+new Date();
}
}
这就是你所需要的。将它部署在 JBoss AS 7.1 上并使用它,比如:
http://127.0.0.1:8080/mywarname/rest/hello
编辑:
我创建了一个具有最小结构的 java war maven 项目:
-pom.xml
-src
|
--main
|
--java
|
--rest
|
--HelloResource.java
--JaxRsActivator.java
我称之为simpleRest
如下所示。所有档案完全如图所示:
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>
<groupId>simpleRest</groupId>
<artifactId>simpleRest</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.1.GA</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<!-- So maven doesn't require web.xml -->
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
HelloResource.java
package rest;
import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("/hello")
public class HelloResource {
@GET
@Produces("text/plain")
public String helloResource() {
return "Hello! It's "+new Date();
}
}
JaxRsActivator.java:
package rest;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/rest")
public class JaxRsActivator extends Application {
}
这会生成一个simpleRest.war
(通过mvn clean package
)。然后我将它部署到新安装的JBoss AS 7.1.1.Final中。如您所见,在部署期间,日志中没有提及 JAX-RS:
22:48:08,677 INFO [org.jboss.as.server.deployment] (MSC service thread 1-1) JBAS015876: Starting deployment of "simpleRest.war"
22:48:09,318 INFO [org.jboss.web] (MSC service thread 1-4) JBAS018210: Registering web context: /simpleRest
22:48:09,492 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "simpleRest.war"
之后,URL 按预期可用:
http://127.0.0.1:8080/simpleRest/rest/hello
请注意,其他所有内容都会出现 404 错误。但它是另一种 404。
http://127.0.0.1:8080/simpleRest/
给出:
HTTP Status 404 - /simpleRest/
那是一个找不到页面的错误。另一方面,http://127.0.0.1:8080/simpleRest/rest
给出:
HTTP Status 404 - Could not find resource for relative : / of full path: http://127.0.0.1:8080/simpleRest/rest
那是找不到资源(REST 服务)错误。这样您就知道 JAX-RS 在起作用,尽管它没有该路径的处理程序。