Stéphane Appercel
Stéphane Appercel

Reputation: 1507

Jersey +Grizzly - @ApplicationPath ignored

I'm running Jersey 2.26-b09 on top of Grizzly, and I'm using the following code to start the Grizzly HTTP server:

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();
}

The RestApplication class extends Application, and has a @ApplicationPath("/system") annotation. The ProductionService class is a REST resource with a @Path("/production") annotation.

I can see that the path specified in the @ApplicationPath is ignored: my resources can be accessed at /rest/production and not at /rest/system/production.

I've tried to change the URI to /rest/system instead of /rest, but to no avail:

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

The application is deployed in the root context /rest, not /rest/system.

What am I missing?

Of course as a workaround I could change the resource path from "/production" to "/system/production", but I would like to know why the application path is ignored.

Upvotes: 1

Views: 669

Answers (1)

St&#233;phane Appercel
St&#233;phane Appercel

Reputation: 1507

I've changed the code that creates and initializes the server to:

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();
}

A Web Application context is created and serves the resources at the desired path. Since the servlet container initializer is not invoked in this programmatic approach, the ServletProperties.JAXRS_APPLICATION_CLASS property is not set.

I thought that setting this property were doing the job, but it does not. Thanks to @peeskillet for the hint.

Upvotes: 1

Related Questions