我在开发期间使用 Endpoint.publish 发布了 JAX-WS 网络服务。是否存在用于在 Jersey 中发布 REST Web 服务的此类实用程序类(在 JAX-RS 中)?我引用了几篇文章,其中大部分是基于在 Jetty、Grizzly 等容器中发布 Web 服务。
最佳答案
Jersey-Grizzly 有一个非常简单的解决方案。来自 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,则需要以下三个依赖项:
<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
。
关于web-services - REST Web 服务的端点发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20802335/