2013-08-19 64 views
4

我想找到一个简单的方法来发布我的REST风格的Web服务使用JAX-RS 2.0与Java SE使用泽西岛和/或Java SE内置HTTP服务器。发布与Java SE REST服务

我想将我的依赖关系降到最低,所以我想避免灰熊,也不想使用任何外部应用程序服务器。

您能否指出我如何发布具有此要求的休息服务?

由于提前,

我的意思是,实现somethig这样的:

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) { 
} 

}

...但避免了灰熊依赖

+0

您还没有提出任何问题(并且推荐工具的请求通常在这里是偏离主题,但可能是服务器故障的主题)。 – chrylis

+0

我已经使用[Simple](http://www.simpleframework.org/)作为实验。它似乎很好地工作。 – OldCurmudgeon

+0

chrylis我认为问题很清楚...但是我已经编辑过,以使其更加明确。 – Rafael

回答

3

如果仅仅依靠

<dependency> 
    <groupId>org.glassfish.jersey.containers</groupId> 
    <artifactId>jersey-container-jdk-http</artifactId> 
    <version>2.2</version> 
</dependency> 

你就可以开始MyApplication的地方延伸ResourceConfig获得资源扫描服务器

JdkHttpServerFactory.createHttpServer(URI.create("http://localhost:8090/root"), 
     new MyApplication()); 

@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(); 
    } 
} 

可能有更好的方法来控制服务器的生命周期,但目前还没有。

+0

看起来够简单了,谢谢......请问,请问我如何正确地扩展ResourceConfig或写下一个非常简单的例子? ...我收到了一些奇怪的问题java.lang.NoSuchMethodError:com.google.common.collect.Sets.newIdentityHashSet()Ljava/util/Set; – Rafael

+0

\t com.google.guava \t 番石榴 \t 14.0.1 fGo

+0

@Rafael我只是延长ResourceConfig,而不是应用,其中包括在我的资源类所在的包。 –