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

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

内容简介:代码日志版权声明:翻译自: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


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

查看所有标签

猜你喜欢:

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

刷新

刷新

[美] 萨提亚·纳德拉 / 陈召强、杨洋 / 中信出版集团 / 2018-1 / 58

《刷新:重新发现商业与未来》是微软CEO萨提亚•纳德拉首部作品。 互联网时代的霸主微软,曾经错失了一系列的创新机会。但是在智能时代,这家科技公司上演了一次出人意料的“大象跳舞”。2017年,微软的市值已经超过6000亿美元,在科技公司中仅次于苹果和谷歌,高于亚马逊和脸谱网。除了传统上微软一直占有竞争优势的软件领域,在云计算、人工智能等领域,微软也获得强大的竞争力。通过收购领英,微软还进入社交......一起来看看 《刷新》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码