2013-08-19 69 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,而不是應用,其中包括在我的資源類所在的包。 –