Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 泽西岛+;Grizzly-@ApplicationPath被忽略_Java_Jersey 2.0_Grizzly - Fatal编程技术网

Java 泽西岛+;Grizzly-@ApplicationPath被忽略

Java 泽西岛+;Grizzly-@ApplicationPath被忽略,java,jersey-2.0,grizzly,Java,Jersey 2.0,Grizzly,我在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); S

我在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();
}
应用程序部署在根上下文/rest中,而不是/rest/system中

我错过了什么


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

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

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();
}
public void start(){
URI=UriBuilder.fromPath(“”.scheme(“http”).host(“localhost”).port(8084.build();
Map params=新的HashMap(16);
字符串applicationPackageName=RestApplication.class.getPackage().getName();
字符串productionPackageName=ProductionService.class.getPackage().getName();
参数put(ServerProperties.PROVIDER_包,productionPackageName+,“+applicationPackageName);
HttpServer服务器=GrizzlyHttpServerFactory.createHttpServer(uri);
WebappContext=newwebappcontext(“system”,即“/rest/system”);
ServletRegistration=context.addServlet(“jersey”,ServletContainer.class);
registration.setInitParameters(params);
registration.addMapping(“/*”);
部署(服务器);
server.start();
}
将创建一个Web应用程序上下文,并以所需的路径为资源提供服务。由于在此编程方法中未调用servlet容器初始值设定项,因此未设置ServletProperties.JAXRS_APPLICATION_类属性


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

注释在ServletContainerInitializer中提取(主要在war部署中)。我不知道灰熊是否支持这一点。也许您需要尝试将应用程序配置为war,并部署war。我从未尝试过与灰熊作战,所以我不知道如何配置它。但我知道初始化器不会被这样的编程配置调用。谢谢@peeskillet。我发现了一个关于如何使用web应用程序以编程方式配置grizzly的示例:。我会给它一个机会,不是这样的。这已经是
GrizzlyWebContainerFactory
在引擎盖下所做的。我的意思是将您的项目打包为WAR,然后配置grizzly以部署该WAR。ApplicationPath实际上是为自动发现的WAR部署而设计的,而不是为嵌入式部署而设计的。没有真正的必要。因为您可以通过编程方式配置URL。
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();
}