REST Web服务的端点发布 [英] Endpoint Publish for REST Web Services
问题描述
在开发过程中,我已经用Endpoint.publish发布了JAX-WS Web服务. (在JAX-RS中)是否存在用于在jersey中发布REST Web服务的此类实用程序类?我提到了几篇文章,其中大多数是基于在某些容器(如Jetty,Grizzly等)中发布Web服务的.
I've published JAX-WS web services with Endpoint.publish during development. Is there any such utility class exists (in JAX-RS) for publishing REST web services in jersey? I referred couple of articles, and majority of them are based on publishing the web services in some containers like Jetty, Grizzly etc.
推荐答案
Jersey-Grizzly有一个非常简单的解决方案.来自 https://github.com/jesperfj/jax-rs-heroku :>
Jersey-Grizzly has a very simple solution. From https://github.com/jesperfj/jax-rs-heroku:
package embedded.rest.server;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.sun.grizzly.http.SelectorThread;
import com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory;
@Path("/hello")
public class Main {
public static void main(String[] args) {
final String baseUri = "http://localhost:7080/";
final Map<String, String> initParams = new HashMap<String, String>();
// Register the package that contains your javax.ws.rs-annotated beans here
initParams.put("com.sun.jersey.config.property.packages","embedded.rest.server");
System.out.println("Starting grizzly...");
try {
SelectorThread threadSelector =
GrizzlyWebContainerFactory.create(baseUri, initParams);
System.out.println(String.format("Jersey started with WADL "
+ "available at %sapplication.wadl.", baseUri));
}
catch(Exception e) {
e.printStackTrace();
}
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Well, this was easy!";
}
}
如果您使用的是Maven,则需要以下三个依赖项:
If you're using Maven, you'll need the following three dependencies:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-grizzly</artifactId>
<version>1.15</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.15</version>
</dependency>
<dependency>
<groupId>com.sun.grizzly</groupId>
<artifactId>grizzly-servlet-webserver</artifactId>
<version>1.9.18-i</version>
</dependency>
要对其进行测试,只需在浏览器中打开http://localhost:7080/hello
.
To test it, just open http://localhost:7080/hello
in a browser.
这篇关于REST Web服务的端点发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!