java 球衣和 jax-rs RI2 - 缺少 HttpServerFactory

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17698899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:43:47  来源:igfitidea点击:

jersey and jax-rs RI2 - missing HttpServerFactory

javajerseyjax-rsgrizzly

提问by rjha94

I have a simple project to test JAX-RS services. Yesterday I downloaded jersey-1.7.1 and used com.sun.jersey.api.container.httpserver.HttpServerFactory and com.sun.net.httpserver.HttpServer to create a http server to test my services from inside eclipse (w/o a heavy weight container)

我有一个简单的项目来测试 JAX-RS 服务。昨天我下载了 jersey-1.7.1 并使用 com.sun.jersey.api.container.httpserver.HttpServerFactory 和 com.sun.net.httpserver.HttpServer 来创建一个 http 服务器来从 eclipse 内部测试我的服务(w/oa 重重量容器)

Today, I downloaded the latest jersey jars (jaxrs-ri) and the HttpServerFactory is missing. Looks like they removed the class between 1.7.1 => 2.0 but I cannot find it in deprecated section. I see grizzly2 classes in API section (maybe that is what I am supposed to use now) but none of the jars in jaxrs-ri bundle provide those classes. I downloaded the jersey-grizzly 1.12 jar but it only has com/sun/jersey/server/impl/container/grizzly2/GrizzlyContainer classes and no implementation.

今天下载了最新的jersey jars(jaxrs-ri),HttpServerFactory不见了。看起来他们删除了 1.7.1 => 2.0 之间的类,但我在已弃用的部分中找不到它。我在 API 部分看到了 grizzly2 类(也许这就是我现在应该使用的)但是 jaxrs-ri 包中的 jar 都没有提供这些类。我下载了 jersey-grizzly 1.12 jar,但它只有 com/sun/jersey/server/impl/container/grizzly2/GrizzlyContainer 类并且没有实现。

So question to kind souls

所以向善良的灵魂提问

1 - with the latest jaxrs-ri jars from jersey download page, what is the recommended way to create a simple http server to test from command line if the 1.7.1 way has been deprecated. what jars to download/include and perhaps a short code sample?

1 - 使用来自 jersey 下载页面的最新 jaxrs-ri jars,如果 1.7.1 方式已被弃用,推荐的方法是创建一个简单的 http 服务器以从命令行进行测试。要下载/包含哪些 jar 包,也许还有一个简短的代码示例?

2 - The whole documentation around creating a simple REST service using java is a big mess. So how do you find the right information?

2 - 关于使用 java 创建简单 REST 服务的整个文档是一团糟。那么如何找到正确的信息呢?

(Really, this is not a joke. maybe this would require a separate blog post - just look at the mess, changed API, no information on deprecated features, implementation diffs, e.g. CXF vs. jersey, API 1.x vs API 2.0, Glassfish container v x.y vs. Tomcat, version x of y vs. version x2 of y, servlet 3.0 would have this file, are you extending Application or not!)

(真的,这不是开玩笑。也许这需要单独的博客文章-看看混乱,更改的 API,没有关于弃用功能的信息,实现差异,例如 CXF 与 jersey,API 1.x 与 API 2.0, Glassfish 容器 v xy 与 Tomcat,y 的 x 版本与 y 的 x2 版本,servlet 3.0 将具有此文件,您是否要扩展应用程序!)

Update

更新

working example with JDKHttp server

JDKHttp 服务器的工作示例

package test.jersey;

import java.net.URI;
import com.sun.net.httpserver.HttpServer;
import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory ;
import org.glassfish.jersey.server.ResourceConfig;

public class ConsoleServerV2 {

    static final String BASE_URI = "http://localhost:9099/";

    public static void main(String[] args) throws Exception {
        HttpServer server = null ;

        ResourceConfig rc = new ResourceConfig(rest.Service.class);
        URI endpoint = new URI(BASE_URI);

        server = JdkHttpServerFactory.createHttpServer(endpoint,rc);
        System.out.println("console v2.0 : Press Enter to stop the server. ");
        System.in.read();
        server.stop(0);

    }
}

采纳答案by stck0177

You could use the JdkHttpServerFactory, which is available in the jersey-container-jdk-http-2.0.jar:

您可以使用 jdkHttpServerFactory,它在jersey-container-jdk-http-2.0.jar 中可用:

ResourceConfig rc = new ResourceConfig(HelloWorldResource.class);
HttpServer server = JdkHttpServerFactory.createHttpServer(baseUri, rc);

No need to call server.start()!

无需调用 server.start()!

回答by sherebry

You can also get the bundle from here http://mvnrepository.com/artifact/com.sun.jersey/jersey-server/1.2Just add it to your library folder then right click on it in the project and select "Add as a library."

您也可以从这里获得包 http://mvnrepository.com/artifact/com.sun.jersey/jersey-server/1.2只需将其添加到您的库文件夹中,然后在项目中右键单击它并选择“添加为图书馆。”