首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jersey +Grizzly - @ApplicationPath已忽略

Jersey +Grizzly - @ApplicationPath已忽略
EN

Stack Overflow用户
提问于 2017-08-19 20:53:34
回答 1查看 526关注 0票数 1

我在Grizzly上运行Jersey 2.26-b09,并使用以下代码启动Grizzly HTTP服务器:

代码语言:javascript
复制
public void start() {
    URI uri = UriBuilder.fromPath("").scheme("http").host("localhost").port(8084).path("/rest").build();
    Map<String, String> params = new HashMap<>(16);
    String applicationClassName = RestApplication.class.getName();
    String applicationPackageName = RestApplication.class.getPackage().getName();
    String productionPackageName = ProductionService.class.getPackage().getName();
    params.put(ServletProperties.JAXRS_APPLICATION_CLASS, applicationClassName);
    params.put(ServerProperties.PROVIDER_PACKAGES, productionPackageName + "," + applicationPackageName);
    HttpServer server = GrizzlyWebContainerFactory.create(uri, params);
    server.start();
}

RestApplication类扩展了应用程序,并有一个@ApplicationPath("/system")注释。Path类是一个带有@ ProductionService (“/production”)注释的REST资源。

我可以看到@ApplicationPath中指定的路径被忽略了:我的资源可以在/rest/production中访问,而不能在/rest/system/production中访问。

我尝试将URI更改为/rest/system而不是/rest,但无济于事:

代码语言:javascript
复制
    URI uri = UriBuilder.fromPath("").scheme("http").host("localhost").port(8084).path("/rest/system").build();

应用程序部署在根上下文/rest中,而不是/rest/system中。

我遗漏了什么?

当然,作为一种变通办法,我可以将资源路径从"/production“更改为"/system/production",但我想知道为什么应用程序路径被忽略。

EN

回答 1

Stack Overflow用户

发布于 2017-08-20 01:00:49

我已经将创建和初始化服务器的代码更改为:

代码语言:javascript
复制
public void start() {
    URI uri = UriBuilder.fromPath("").scheme("http").host("localhost").port(8084).build();
    Map<String, String> params = new HashMap<>(16);
    String applicationPackageName = RestApplication.class.getPackage().getName();
    String productionPackageName = ProductionService.class.getPackage().getName();
    params.put(ServerProperties.PROVIDER_PACKAGES, productionPackageName + "," + applicationPackageName);

    HttpServer server = GrizzlyHttpServerFactory.createHttpServer(uri);
    WebappContext context = new WebappContext("system", "/rest/system");
    ServletRegistration registration = context.addServlet("jersey", ServletContainer.class);
    registration.setInitParameters(params);
    registration.addMapping("/*");
    context.deploy(server);

    server.start();
}

将创建Web应用程序上下文,并为所需路径上的资源提供服务。由于在此编程方法中未调用servlet容器初始值设定项,因此未设置ServletProperties.JAXRS_APPLICATION_CLASS属性。

我以为设置这个属性就行了,但事实并非如此。感谢@peeskillet的提示。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45771768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档