我正在尝试找到一种使用 JAX-RS 2.0 和 Java SE 使用 Jersey 和/或 Java SE 内置 http 服务器发布我的 RESTful Web 服务的简单方法。
我想将我的依赖性保持在最低限度,所以我想避免 grizzly,也不想使用任何外部应用程序服务器。
你能告诉我如何根据这个要求发布休息服务吗?
提前致谢
我的意思是实现这样的目标:
public static void main(String args[]) {
try {
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer("http://localhost:8080/calculator/",new ResourceConfig(SumEndpoint.class));
System.in.read();
server.stop();
} catch (IOException ex) {
}
...但要避免灰熊依赖
最佳答案
如果你只是依赖
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>2.2</version>
</dependency>
然后就可以启动服务器了
JdkHttpServerFactory.createHttpServer(URI.create("http://localhost:8090/root"),
new MyApplication());
其中 MyApplication 扩展 ResourceConfig 以获取资源扫描。
@ApplicationPath("/")
public class MyApplication extends ResourceConfig {
public MyApplication() {
packages("...");
}
@GET
@Produces("text/plain")
public Response foo() {
return Response.ok("Hey, it's working!\n").build();
}
}
可能有更好的方法来控制服务器生命周期,但我暂时还不知道。
关于java - 使用 Java SE 发布 REST 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18314608/