带嵌入式服务器的JAX-RS [英] JAX-RS with embedded server

查看:128
本文介绍了带嵌入式服务器的JAX-RS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

有没有办法使服务器gzip响应内容,如果接受编码:gzip 是否存在?

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

nicore 提供的样本确实有效,它允许你制作JAX -RS风格的服务器位于嵌入式轻量级服务器之上,没有servlet容器,但是有一些时刻需要考虑。

Samples provided by nicore actually 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.

如果你喜欢自己管理类(并保存)在启动期间,您可以使用以下内容:

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

JAX-RS hello world cl屁股:

JAX-RS hello world class:

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



已解决的依赖项:



简单:

Resolved dependencies:

Simple:

  • Simple Framework itself
  • jersey-simple-server

Grizzly:

  • grizzly-framework
  • grizzly-http
  • grizzly-http-server (different repository!)
  • jersey-grizzly2

泽西岛:

  • jersey-archive

确保 javax.ws.rs 存档没有进入你的类路径,因为它与泽西岛的实现相冲突。这里最糟糕的是没有记录的静默404错误 - 只记录 FINER 级别的小记录。

Make sure the javax.ws.rs archive 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 FINER level is logged.

推荐答案

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

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

如果主要关注的是对servlet容器的依赖,可以使用JAX-RS RuntimeDelegate 将您的应用程序注册为JAX-RS端点。

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

// Using grizzly as the underlaying server
SelectorThread st = RuntimeDelegate.createEndpoint(new MyApplication(), SelectorThread.class);

st.startEndpoint();

// Wait...
st.stopEndpoint();

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

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

编辑

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

I did some small tests. The following two things will definitely work for you, assuming you are using 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,但我现在无法回想起它。

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

这篇关于带嵌入式服务器的JAX-RS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆