内容简介:代码日志版权声明:翻译自: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 本身
灰熊:
> grizzly-http-server (不同的仓库!)
球衣号码:
注意
确保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
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Beginning ASP.NET 4 in C# and Vb
Imar Spaanjaars / Wrox / 2010-3-19 / GBP 29.99
This book is for anyone who wants to learn how to build rich and interactive web sites that run on the Microsoft platform. With the knowledge you gain from this book, you create a great foundation to ......一起来看看 《Beginning ASP.NET 4 in C# and Vb》 这本书的介绍吧!