17

澄清:这个问题是关于 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();
    }
}

已解决的依赖关系:

简单的:

灰熊:

球衣:

注意

确保javax.ws.rs存档没有进入您的类路径,因为它与 Jersey 的实现冲突。这里最糟糕的是没有记录的无声 404 错误 - 只记录了一个关于FINER级别的小注释。

4

3 回答 3

16

如果你真的想用 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 提供了一个过滤器来透明地完成编码。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 依赖项

<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>

或使用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 依赖项

<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,但我现在想不起来了。

于 2011-11-26T10:12:53.913 回答
0

压缩输出是 JAX WS 实现的责任。您应该参考服务器的(Tomcat、Glassfish、JBoss 等)文档来配置您的 http 网络侦听器。

于 2011-11-26T15:00:21.487 回答
0

如果将 CXF 用于您的 JAX-WS 实现(或 JAX-RS),您只需将 @GZIP 注释添加到服务类。

于 2011-11-26T16:36:11.203 回答