Ale*_*rov 17 java rest jax-rs embedded-server
澄清:这个问题是关于GZIPping基于JAX-WS的REST服务,但我决定改变主题以便更容易找到
我正在通过JAX-WS实现REST服务Provider <Source>
,并使用标准发布它Endpoint
(原因是我想避免使用servlet容器或应用程序服务器).
有没有办法使服务器gzip响应内容,如果Accept-Encoding: gzip
存在?
nicore
实际提供的示例工作,它允许您在没有servlet容器的嵌入式轻量级服务器之上制作JAX-RS样式的服务器,但是几乎没有时间可以考虑.
如果您希望自己管理类(并在启动期间节省时间),则可以使用以下命令:
JAX-RS你好世界级:
@Path("/helloworld")
public class RestServer {
@GET
@Produces("text/html")
public String getMessage(){
System.out.println("sayHello()");
return "Hello, world!";
}
}
Run Code Online (Sandbox Code Playgroud)
主要方法:
对于简单服务器:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
对于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();
}
}
Run Code Online (Sandbox Code Playgroud)
简单:
灰熊:
球衣号码:
确保javax.ws.rs
存档没有进入您的类路径,因为它与Jersey的实现冲突.这里最糟糕的是没有记录的无声404错误 - 只记录了一个关于FINER
级别的小记录.
nre*_*nre 17
如果你真的想用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();
Run Code Online (Sandbox Code Playgroud)
关于GZIP
编码,每个JAX-RS提供商都有不同的方法.Jersey提供了一个过滤器来透明地完成编码.RESTEasy 为此提供了注释.
编辑
我做了一些小测试.假设您正在使用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 {
;
}
}
}
Run Code Online (Sandbox Code Playgroud)
与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>
Run Code Online (Sandbox Code Playgroud)
或者使用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 {
;
}
}
}
Run Code Online (Sandbox Code Playgroud)
与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>
Run Code Online (Sandbox Code Playgroud)
老实说,我也无法让RuntimeDelegate
样本正常工作.肯定有一种方法可以开箱即用启动RESTEasy,但我现在无法回想起它.
归档时间: |
|
查看次数: |
21495 次 |
最近记录: |