我在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类扩展了应用程序,并有一个@ApplicationPath("/system")注释。Path类是一个带有@ ProductionService (“/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",但我想知道为什么应用程序路径被忽略。
发布于 2017-08-20 01:00:49
我已经将创建和初始化服务器的代码更改为:
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的提示。
https://stackoverflow.com/questions/45771768
复制相似问题