加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 08:45:30 所属栏目:Java 来源:网络整理
导读:我正在查看 Jersey User Guide,并尝试使用Jersey Web服务和嵌入式Grizzly服务器设置Hello World示例. 我正在完成第1节“入门”.我已经在1.1节编译中得到了代码示例: // The Java class will be hosted at the URI path "/helloworld" @Path("/helloworld")
我正在查看 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.wadln” + 
           “Try out %shelloworldnHit 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对类似问题的回答.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读