Java 带有嵌入式服务器的 JAX-RS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8277409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-15 00:34:45  来源:igfitidea点击:

JAX-RS with embedded server

javarestjax-rsembedded-server

提问by Alex Abdugafarov

Clarification:this question was about GZIPping an JAX-WS-based REST service, but I've decided to change the topic to make it easier to find

澄清:这个问题是关于 GZIPping 一个基于 JAX-WS 的 REST 服务,但我决定更改主题以使其更容易找到

I'm implementing a REST service via JAX-WS Provider <Source>, and publishing it with standard Endpoint(the reason is that I want to avoid using a servlet container or application server).

我正在通过 JAX-WS 实现 REST 服务Provider <Source>,并使用标准发布它Endpoint(原因是我想避免使用 servlet 容器或应用程序服务器)。

Is there a way to make server to gzip response content, if Accept-Encoding: gzipis present?

有没有办法让服务器 gzip 响应内容(如果Accept-Encoding: gzip存在)?



HOW-TO

如何

Samples provided by nicoreactually works, and it allows you to make JAX-RS-styled server on top of embedded lightweight server without servlet container, but there are few moments to be considered.

提供的示例nicore确实有效,它允许您在没有 servlet 容器的嵌入式轻量级服务器之上制作 JAX-RS 风格的服务器,但需要考虑的时间很少。

If you prefer to manage classes by yourself (and save a time during startup), you may use the following:

如果您更喜欢自己管理类(并在启动时节省时间),您可以使用以下方法:

Example

例子

JAX-RS hello world class:

JAX-RS 你好世界级:

@Path("/helloworld")
public class RestServer {

    @GET
    @Produces("text/html")
    public String getMessage(){
        System.out.println("sayHello()");
        return "Hello, world!";
    }
}

Main method:

主要方法:

For SimpleServer:

对于简单服务器:

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();
    }
}

For Grizzly2:

对于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();
    }
}

Resolved dependencies:

已解决的依赖项:

Simple:

简单的:

Grizzly:

灰熊:

Jersey:

球衣:

Notice

注意

Make sure the javax.ws.rsarchive didnt get into your classpath, as it conflicts with Jersey's implementation. The worst thing here is a silent 404 error with no logging - only a small note on FINERlevel is logged.

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

采纳答案by nre

If you really want to do REST with Java I would suggest you to to use a JAX-RS implementation (RESTeasy, Jersey...).

如果你真的想用 Java 做 REST,我建议你使用 JAX-RS 实现(RESTeasy、Jersey ...)。

If your main concern is the dependency on a servlet container, you could use the JAX-RS RuntimeDelegateto register your application as a JAX-RS endpoint.

如果您主要关心的是对 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();

Concerning GZIPencoding, each JAX-RS provider has different approaches. Jersey provides a filterto accomplish the encoding transparently. RESTEasy provides an annotation for that.

关于GZIP编码,每个 JAX-RS 提供者都有不同的方法。Jersey 提供了一个过滤器来透明地完成编码。RESTEasy为此提供了注释

EDIT

编辑

I did some small tests. The following two things will definitely work for you, assuming you are using Maven.

我做了一些小测试。假设您使用的是Maven,以下两件事肯定对您有用

Using Jersey + SimpleServer:

使用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 {
            ;
        }
    }
}

with maven dependencies

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

Or using the Jersey + Grizzly2:

或者使用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 {
            ;
        }
    }
}

with maven dependencies

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

Honestly speaking I was not able to get the RuntimeDelegatesample working, too. There certainly is a way to start RESTEasy out of the box, too but I cannot recall it at the moment.

老实说,我也无法让RuntimeDelegate样本工作。当然也有一种方法可以开箱即用地启动 RESTEasy,但我现在想不起来了。

回答by andbi

gzipping the output is the reponsibility of JAX WS implementation. You should refer to server's (Tomcat, Glassfish, JBoss, etc) documentation in order to configure your http network listeners.

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

回答by Daniel Kulp

If using CXF for your JAX-WS implementation (or JAX-RS), you could just add @GZIP annotation onto the service class.

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