17

明確化:この質問は JAX-WS ベースの REST サービスの GZIPping に関するものでしたが、見つけやすくするためにトピックを変更することにしました

JAX-WS 経由で REST サービスを実装しProvider <Source>、標準で公開していますEndpoint(理由は、サーブレット コンテナーまたはアプリケーション サーバーの使用を避けたいためです)。

存在する場合、サーバーを gzip 応答コンテンツにする方法はありAccept-Encoding: gzipますか?


方法

によって提供されるサンプルはnicore実際に機能し、サーブレット コンテナーを使用せずに組み込みの軽量サーバー上に 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();
    }
}

解決された依存関係:

単純:

グリズリー:

ジャージー:

知らせ

javax.ws.rsJersey の実装と競合するため、アーカイブがクラスパスに入っていないことを確認してください。ここで最悪なのは、ログなしのサイレント 404 エラーFINERです。レベルに関する小さなメモのみがログに記録されます。

4

3 に答える 3

16

本当にJavaでRESTを実行したい場合は、JAX-RS実装(RESTeasy、Jersey ...)を使用することをお勧めします。

主な関心事がサーブレットコンテナへの依存である場合は、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プロバイダーごとに異なるアプローチがあります。ジャージーは、エンコーディングを透過的に実行するためのフィルターを提供します。RESTEasyは、そのための注釈を提供します

編集

私はいくつかの小さなテストをしました。Mavenを使用していると仮定すると、次の2つのことが確実に機能します。

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

出力をgzipすることは、JAXWS実装の責任です。httpネットワークリスナーを設定するには、サーバー(Tomcat、Glassfish、JBossなど)のドキュメントを参照する必要があります。

于 2011-11-26T15:00:21.487 に答える
0

JAX-WS 実装 (または JAX-RS) に CXF を使用している場合は、@GZIP アノテーションをサービス クラスに追加するだけです。

于 2011-11-26T16:36:11.203 に答える