3

我正在查看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 个类。

4

1 回答 1

7

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

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

非 Maven 开发人员需要:

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

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

于 2011-11-22T13:34:54.307 回答