java – JAX-RS带嵌入式服务器

栏目: Java · 发布时间: 7年前

内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/8277409/jax-rs-with-embedded-server

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

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

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

如何

由nicore提供的示例实际上是有效的,它允许您将JAX-RS风格的服务器放在没有servlet容器的嵌入式轻量级服务器之上,但是很少有时间被考虑.

如果您喜欢自己管理课程(并在启动过程中节省时间),则可以使用以下内容:

JAX-RS你好世界级:

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

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

主要方法:

对于 Simple 服务器:

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

已解决依赖关系:

简单:

> Simple Framework 本身

> jersey-simple-server

灰熊:

> grizzly-framework

> grizzly-http

> grizzly-http-server (不同的仓库!)

> jersey-grizzly2

球衣号码:

> jersey-archive

注意

确保javax.ws.rs存档没有进入您的类路径,因为它与Jersey的实现有冲突.这里最糟糕的是一个无声的404错误,没有记录 – 只有FINER级别上的一个小笔记才被记录下来.

如果您真的想用 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提供商都有不同的方法.泽西提供 a filter 完成编码透明度. RESTEasy provides an annotation for that .

编辑

我做了一些小测试.假设您正在使用 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>

或使用泽西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开箱即用,但我现在不记得了.

代码日志版权声明:

翻译自:http://stackoverflow.com/questions/8277409/jax-rs-with-embedded-server


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

矩阵计算

矩阵计算

Gene H. Golub、Charles F. Van Loan / 袁亚湘 / 人民邮电出版社 / 2009 / 89.00元

本书是国际上数值计算方面的权威著作,有“圣经”之称。被美国加州大学、斯坦福大学、华盛顿大学、芝加哥大学、中国科学院研究生院等很多世界知名学府用作相关课程的教材或主要参考书。 本书系统地介绍了矩阵计算的基本理论和方法。书中的许多算法都有现成的软件包实现,每节后还附有习题,并有注释和大量参考文献,非常有助于自学。一起来看看 《矩阵计算》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

随机密码生成器
随机密码生成器

多种字符组合密码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具