【发布时间】: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