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


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

查看所有标签

猜你喜欢:

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

程序设计基础

程序设计基础

谢书良 / 2010-5 / 29.50元

《程序设计基础》是为从来没有接触过程序设计的读者编写的“零起点”入门教材。全书共分8章,第1章主要介绍程序设计的概念和程序运行的环境,第2章介绍了基本的数据类型、运算符与表达式,第3章介绍面向过程程序的顺序、分支选择和循环三种控制结构,第4章至第7章分别介绍了数组、指针的概念,结构体和其他数据类型,函数及其调用,内容涵盖了C++面向过程程序设计内容,与C语言教材完全兼容。第8章是体现《程序设计基础......一起来看看 《程序设计基础》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

SHA 加密
SHA 加密

SHA 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换