You can manage Resource instantiation yourself overriding Application#getSingletons:
@ApplicationPath("/r")
public class RestApplication extends Application {
@Override
public Set<Object> getSingletons() {
Foo foo = new Foo();
Bar bar = new Bar(42);
return new HashSet<Object>(Arrays.asList(foo, bar));
}
}
But therefore you'll need two classes, each one with the full path:
@Path("/foo/isAlive")
public class Foo {
public Foo() {}
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response isAlive() {
return Response.ok("foo is alive").build();
}
}
@Path("/foo/getConfigFromDB")
public class Foo2 {
private int n;
public Foo2(int n) {
this.n = n;
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response isAlive() {
return Response.ok("bar initialized with " + n).build();
}
}
You could also use a Subresource:
@Path("/foo")
public class Foo {
public Foo() {}
@GET
@Path("/isAlive")
@Produces(MediaType.TEXT_PLAIN)
public Response isAlive() {
return Response.ok("foo is alive").build();
}
@Path("/getConfigFromDB")
@Produces(MediaType.TEXT_PLAIN)
public Bar getConfigFromDB() {
return new Bar(4711);
}
}
public class Bar {
private int n;
public Bar(int n) {
this.n = n;
}
@GET
public Response get() {
return Response.ok("Bar initialized with " + n).build();
}
}
But if your problem is about getting authentification-information in the second method as you wrote in a comment I would not use the constructor anyway.
See this answer for some other examples.