Hello World 与 Jersey 和 Grizzly(来自用户指南)

发布于 2024-12-17 16:14:15 字数 1897 浏览 1 评论 0原文

我正在查看 Jersey 用户指南 并尝试设置使用 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 个类。

I'm looking at the Jersey User Guide and trying to set up a Hello World example using a Jersey web service and an embedded Grizzly server.

I'm running through Section 1 "Getting Started". I've got the code example in section 1.1 compiling just fine:

// 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";
     }
 }

But then I get to section 1.2, "Deploying the Root Resource", which is where I'm supposed to set up an embedded Grizzly web server to test my resource with:

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

The problem is, it seems this user guide has not been updated in awhile and the class GrizzlyWebContainerFactory no longer exists!

I'm using Jersery v 1.10 and Grizzly v 1.9.41.

Can someone help me recreate this example? I know I can run the web service in a container, I'm interested in running it via the simplest possible embedded server setup that requires no additional resources (web.xml, etc.) in my project, just the 2 classes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渔村楼浪 2024-12-24 16:14:15

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

用户指南提供的所需依赖项列表中指定了这一点:

非 Maven 开发人员需要:

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

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

I think the answer is that I need to include the jersey-grizzly dependency, then I can follow along as per the user guide.

This is NOT specified in the list of required dependencies the user guide provides:

Non-maven developers require:

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

Thanks to Ryan Stewart's answer to a similar question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文