栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

如果您真的想使用Java进行REST,建议您使用JAX-RS实现(RESTeasy,Jersey …)。

如果您主要关注对servlet容器的依赖,则可以使用JAX-RS
RuntimeDelegate将应用程序注册为JAX-
RS端点。

// Using grizzly as the underlaying serverSelectorThread st = RuntimeDelegate.createEndpoint(new MyApplication(), SelectorThread.class);st.startEndpoint();// Wait...st.stopEndpoint();

关于

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 { ;        }    }}

与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,但是目前我还不记得它。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/507433.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号