【问题标题】:Jersey +Grizzly - @ApplicationPath ignoredJersey +Grizzly - @ApplicationPath 被忽略
【发布时间】:2018-01-28 00:47:05
【问题描述】:

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

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 类扩展了 Application,并有一个 @ApplicationPath("/system") 注解。 ProductionService 类是一个带有 @Path("/production") 注释的 REST 资源。

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

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

    URI uri = UriBuilder.fromPath("").scheme("http").host("localhost").port(8084).path("/rest/system").build();

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

我错过了什么?

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

【问题讨论】:

  • 注释在 ServletContainerInitializer 中提取(主要在战争部署中)。我不知道灰熊是否支持这一点。也许您需要尝试将应用程序配置为战争,并部署战争。我从未尝试过与 grizzly 进行战争,所以我不确定如何配置它。但我知道初始化程序不会被这样的编程配置调用。
  • 谢谢@peeskillet。我找到了一个关于如何使用 Web 应用程序以编程方式配置 grizzly 的示例:stackoverflow.com/questions/22593983/…。我会给它一个机会。
  • 不是这样的。这已经是GrizzlyWebContainerFactory 在幕后所做的。我的意思是将您的项目打包为 WAR,然后配置 grizzly 来部署该战争。 ApplicationPath 实际上是为自动发现的 WAR 部署而不是嵌入式部署而设计的。没有真正的需要。因为您可以通过编程方式配置 URL。

标签: java jersey-2.0 grizzly


【解决方案1】:

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

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 的提示。

【讨论】:

    猜你喜欢
    • 2019-09-06
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 1970-01-01
    • 2013-09-09
    相关资源
    最近更新 更多