java – JAX-RS帶嵌入式伺服器
澄清:這個問題是關於GZIPping基於JAX-WS的REST服務,但是我決定改變主題,以便更容易找到
我正在通過JAX-WS Provider<Source>實現REST服務,並用標準的Endpoint釋出它(原因是我想避免使用servlet容器或應用程式伺服器).
有沒有辦法使伺服器gzip響應內容,如果Accept-Encoding:gzip存在?
如何
由nicore提供的示例實際上是有效的,它允許您將JAX-RS風格的伺服器放在沒有servlet容器的嵌入式輕量級伺服器之上,但是很少有時間被考慮.
如果您喜歡自己管理課程(並在啟動過程中節省時間),則可以使用以下內容:
例
JAX-RS你好世界級:
@Path("/helloworld") public class RestServer { @GET @Produces("text/html") public String getMessage(){ System.out.println("sayHello()"); return "Hello, world!"; } }
主要方法:
對於ofollow,noindex" target="_blank">Simple 伺服器:
public static void main(String[] args) throws Exception{ DefaultResourceConfig resourceConfig = new DefaultResourceConfig(RestServer.class); // The following line is to enable GZIP when client accepts it resourceConfig.getContainerResponseFilters().add(new GZIPContentEncodingFilter()); Closeable server = SimpleServerFactory.create("http://0.0.0.0:5555", resourceConfig); try { System.out.println("Press any key to stop the service..."); System.in.read(); } finally { server.close(); } }
對於Grizzly2 :
public static void main(String[] args) throws Exception{ DefaultResourceConfig resourceConfig = new DefaultResourceConfig(RestServer.class); // The following line is to enable GZIP when client accepts it resourceConfig.getContainerResponseFilters().add(new GZIPContentEncodingFilter()); HttpServer server = GrizzlyServerFactory.createHttpServer("http://0.0.0.0:5555" , resourceConfig); try { System.out.println("Press any key to stop the service..."); System.in.read(); } finally { server.stop(); } }
已解決依賴關係:
簡單:
>Simple Framework 本身
灰熊:
>grizzly-http-server (不同的倉庫!)
球衣號碼:
注意
確保javax.ws.rs存檔沒有進入您的類路徑,因為它與Jersey的實現有衝突.這裡最糟糕的是一個無聲的404錯誤,沒有記錄 – 只有FINER級別上的一個小筆記才被記錄下來.
如果您真的想用Java做REST,建議您使用JAX-RS實現(RESTeasy,Jersey …).
如果您的主要關注點是對servlet容器的依賴,那麼可以使用JAX-RSRuntimeDelegate 將應用程式註冊為JAX-RS端點.
// Using grizzly as the underlaying server SelectorThread st = RuntimeDelegate.createEndpoint(new MyApplication(), SelectorThread.class); st.startEndpoint(); // Wait... st.stopEndpoint();
關於GZIP編碼,每個JAX-RS提供商都有不同的方法.澤西提供a filter 完成編碼透明度. RESTEasyprovides an annotation for that .
編輯
我做了一些小測試.假設您正在使用Maven ,以下兩件事一定會為您工作.
使用Jersey SimpleServer:
public static void main( String[] args ) throws Exception { java.io.Closeable server = null; try { // Creates a server and listens on the address below. // Scans classpath for JAX-RS resources server = SimpleServerFactory.create("http://localhost:5555"); System.out.println("Press any key to stop the service..."); System.in.read(); } finally { try { if (server != null) { server.close(); } } finally { ; } } }
與maven依賴關係
<dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.10</version> </dependency> <dependency> <groupId>com.sun.jersey.contribs</groupId> <artifactId>jersey-simple-server</artifactId> <version>1.10</version> </dependency>
或使用澤西Grizzly2:
public static void main(String[] args) throws Exception { HttpServer server = null; try { server = GrizzlyServerFactory.createHttpServer("http://localhost:5555"); System.out.println("Press any key to stop the service..."); System.in.read(); } finally { try { if (server != null) { server.stop(); } } finally { ; } } }
與maven依賴關係
<dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.10</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-grizzly2</artifactId> <version>1.10</version> </dependency>
老實說,我還沒有得到RuntimeDelegate樣本的工作.
肯定有一種方法可以啟動RESTEasy開箱即用,但我現在不記得了.
程式碼日誌版權宣告:
翻譯自:http://stackoverflow.com/questions/8277409/jax-rs-with-embedded-server