带嵌入式服务器的JAX-RS
澄清:这个问题是关于GZIPping基于JAX-WS的REST服务,但我决定改变主题以便更容易找到
我正在通过JAX-WS Provider
实现REST服务,并使用标准Endpoint
发布它(原因是我想避免使用servlet容器或应用程序服务器)。
有没有办法使服务器gzip响应内容,如果Accept-Encoding: gzip
存在?
如何
由nicore
提供的nicore
实际上是nicore
,它允许您在没有servlet容器的情况下在嵌入式轻量级服务器之上制作JAX-RS样式的服务器,但是有一些时刻需要考虑。
如果您更喜欢自己管理类(并在启动期间节省时间),您可以使用以下内容:
例
JAX-RS你好世界级:
@Path("/helloworld") public class RestServer { @GET @Produces("text/html") public String getMessage(){ System.out.println("sayHello()"); return "Hello, world!"; } }
主要方法:
对于简单服务器:
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(); } }
已解决的依赖项:
简单:
- 简单框架本身
- 新泽西简单的服务器
灰熊:
- 灰熊框架
- Grizzly的HTTP
- grizzly-http-server (不同的存储库!)
- jersey,grizzly2
jersey号码:
- jersey归档
注意
确保javax.ws.rs
存档没有进入您的类路径,因为它与Jersey的实现冲突。 这里最糟糕的是没有记录的无声404错误 – 只记录了FINER
级别的小注释。
如果你真的想用Java做REST,我建议你使用JAX-RS实现(RESTeasy,Jersey ……)。
如果主要关注的是对servlet容器的依赖,可以使用JAX-RS RuntimeDelegate将应用程序注册为JAX-RS端点。
// Using grizzly as the underlaying server SelectorThread st = RuntimeDelegate.createEndpoint(new MyApplication(), SelectorThread.class); st.startEndpoint(); // Wait... st.stopEndpoint();
关于GZIP
编码,每个JAX-RS提供商都有不同的方法。 Jersey提供了一个filter来透明地完成编码。 RESTEasy 为此提供了注释 。
编辑
我做了一些小测试。 假设您正在使用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依赖
com.sun.jersey jersey-core 1.10 com.sun.jersey.contribs jersey-simple-server 1.10
或者使用Jersey + 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依赖
com.sun.jersey jersey-core 1.10 com.sun.jersey jersey-grizzly2 1.10
老实说,我也无法让RuntimeDelegate
示例正常工作。 肯定有一种开箱即用的方式可以启动RESTEasy,但我现在无法回忆起它。
gzipping输出是JAX WS实现的可靠性。 您应该参考服务器(Tomcat,Glassfish,JBoss等)文档来配置您的http网络侦听器。
如果将CXF用于JAX-WS实现(或JAX-RS),则只需将@GZIP注释添加到服务类中即可。