gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:40:30 24 4
gpt4 key购买 nike

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

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

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


操作方法

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

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

例子

JAX-RS Hello World 类:

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

已解决的依赖关系:

简单:

灰熊:

Jersey :

通知

确保 javax.ws.rs archive 没有进入你的类路径,因为它与 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 提供者都有不同的方法。 Jersey 提供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>

或者使用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,但我现在想不起来了。

关于java - 带有嵌入式服务器的 JAX-RS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8277409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com