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

标签 java web-services jersey grizzly

我正在查看 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/

相关文章:

java - XJC 与 Maven 的片段

web-services - SoapUI负载测试,计算方差策略中的cnt

cookies - Jersey:我可以在 ContainerResponseFilter 中添加 cookie 吗?

jersey - OSGi、Jersey 和 "No message body writer has been found for class"

java - Spring 启动: Create EAR from springboot multi-module project

Java 引用 ArrayList

java - 如何确保 gradle 中的项目具有相同的 jar 版本?

java - Spring Boot CRUD操作创建Web服务配置

java - 如何从返回 dataset(<Object> any) 的 Web 服务创建 POJO

java - 是否可以将 Spring MVC 与 Jersey 注解一起使用?