澄清:这个问题是关于 GZIPping 基于 JAX-WS 的 REST 服务,但我决定更改主题以使其更容易找到
我正在通过 JAX-WS 实现 REST 服务Provider <Source>
,并使用标准发布它Endpoint
(原因是我想避免使用 servlet 容器或应用程序服务器)。
有没有办法让服务器 gzip 响应内容(如果Accept-Encoding: gzip
存在)?
如何
提供的示例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();
}
}
对于灰熊2:
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();
}
}
已解决的依赖关系:
简单的:
灰熊:
- 灰熊框架
- 灰熊-http
- grizzly-http-server(不同的存储库!)
- 泽西-灰熊2
球衣:
注意
确保javax.ws.rs
存档没有进入您的类路径,因为它与 Jersey 的实现冲突。这里最糟糕的是没有记录的无声 404 错误 - 只记录了一个关于FINER
级别的小注释。