Skip to content

Resource installer

CoreInstallersBundle / ResourceInstaller

Installs rest resources.

Recognition

Detects classes annotated with jax-rs @Path annotation and register them as rest resources. Guice will manage resource creation, so you may think of it as usual guice bean.

@Path("/res")
@Produces('application/json')
class SampleResource {

    @Inject
    private MyService service;

    @GET
    @Path("/sample")
    public Response sample() {
        return Response.ok(service.result()).build();
    }
}

Resources registered as singletons, when no explicit scope annotation is used. Behaviour could be disabled with option:

.option(InstallerOptions.ForceSingletonForJerseyExtensions, false)

Special @Prototype scope annotation may be used to mark resources in prototype scope. It is useful when guice servlet support is disabled (and so @RequestScoped could not be used).

Interface recognition

Class will also be recognized if @Path annotation found on directly implemented interface.

@Path("/res")
@Produces('application/json')
interface ResourceContract {

    @GET
    @Path("/sample")
    String sample();
}

class SampleResource implements ResourceContract {

    @Inject
    private MyService service;

    @Override
    public Response sample() {
        return Response.ok(service.result()).build();
    }
}

Annotations on interfaces are useful for jersey client proxies

Client client = ClientBuilder.newClient();
ResourceContract resource = WebResourceFactory
    .newResource(ResourceContract.class, client.target("http://localhost:8080/"));

// call sample method on remote resource http://localhost:8080/res/sample
String result = resource.sample();

Jersey client proxies requires extra dependency org.glassfish.jersey.ext:jersey-proxy-client

Request scope bindings

If you need request scoped objects, use Provider:

class SampleResource {

    @Inject
    private Provider<HttpServletRequest> requestProvider;

    @GET
    @Path("/sample")
    public Response sample() {
        HttpServletRequest request = requestProvider.get();
        ...
    }

See jersey objects, available for injection.

@Context usage

@Context annotation usage is a common point of confusion. You can't use it for class fields:

this will not work

public class MyResource {
    @Context UriInfo info;
}

Use provider instead:

correct way

public class MyResource {
    @Inject Provider<UriInfo> infoProvider;
}

But, you can use @Context on method parameters:

public class MyResource {
    @GET
    public Response get(@Context UriInfo info) { ... }
}

Jersey managed resource

If resource class is annotated with @JerseyManaged then jersey HK2 container will manage bean creation instead of guice. Injection of guice managed beans could still be possible via registered HK2-guice-bridge, but guice aop features will not work.

Note

You can manage resources with HK2 by default, but this will also affect all jersey extensions

@Path("/res")
@Produces('application/json')
@JerseyManaged
class SampleResource {
    ...
}

@Context annotation on field will work on HK2 managed bean:

@Path()
@JerseyManaged
public class MyResource {
    @Context UriInfo info;
}