gpt4 book ai didi

java - 使用 Jersey 和 Grizzly 的 Hello World(来自用户指南)

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:32:37 25 4
gpt4 key购买 nike

我正在查看 Jersey User Guide并尝试使用 Jersey Web 服务和嵌入式 Grizzly 服务器设置 Hello World 示例。

我正在浏览第 1 部分“入门”。我在 1.1 节中的代码示例编译得很好:

// The Java class will be hosted at the URI path "/helloworld"
@Path("/helloworld")
public class HelloWorldResource {

// The Java method will process HTTP GET requests
@GET
// The Java method will produce content identified by the MIME Media
// type "text/plain"
@Produces("text/plain")
public String getClichedMessage() {
// Return some cliched textual content
return "Hello World";
}
}

但随后我进入了第 1.2 节“部署根资源”,我应该在这里设置一个嵌入式 Grizzly Web 服务器来测试我的资源:

public class Main {

public static void main(String[] args) throws IOException {

final String baseUri = "http://localhost:9998/";
final Map<String, String> initParams =
new HashMap<String, String>();

initParams.put("com.sun.jersey.config.property.packages",
"com.sun.jersey.samples.helloworld.resources");

System.out.println("Starting grizzly...");
SelectorThread threadSelector =
GrizzlyWebContainerFactory.create(baseUri, initParams);
System.out.println(String.format(
"Jersey app started with WADL available at %sapplication.wadl\n” +
“Try out %shelloworld\nHit enter to stop it...", baseUri, baseUri));
System.in.read();
threadSelector.stopEndpoint();
System.exit(0);
}
}

问题是,这个用户指南似乎已经有一段时间没有更新了,GrizzlyWebContainerFactory 类已经不存在了!

我使用的是 Jersery v 1.10 和 Grizzly v 1.9.41。

有人可以帮我重现这个例子吗?我知道我可以在容器中运行 Web 服务,我有兴趣通过最简单的嵌入式服务器设置来运行它,在我的项目中不需要额外的资源(web.xml 等),只需要 2 个类。

最佳答案

我认为答案是我需要包含 jersey-grizzly依赖项,然后我可以按照用户指南进行操作。

这在用户指南提供的必需依赖项列表中没有指定:

Non-maven developers require:

grizzly-servlet-webserver.jar, jersey-server.jar, jersey-core.jar, jsr311-api.jar, asm.jar

感谢Ryan Stewart对类似问题的回答。

关于java - 使用 Jersey 和 Grizzly 的 Hello World(来自用户指南),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8226688/

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