public class ShoppingApplication extends Application {
private Set<Object> singletons = new HashSet<>();
private Set<Class<?>> classes = new HashSet<>();
public ShoppingApplication() {
classes.add(CustomerResourceService.class);
classes.add(JAXBMarshaller.class);
classes.add(JSONMarshaller.class);
singletons.add(new CustomerResourceService());
}
@Override
public Set<Class<?>> getClasses() {
return classes;
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
Suppose I have above code which I extends Application and register my resources or providers to set. I want to know how can I dynamically inject my resources to set in runtime, my web application will create several new resources in runtime and need to inject to Application inorder to use.
Programmatic API for building resources
The
Resource
class is what your are looking for. Just mind it's a Jersey specific API.According to the documentation, the
Resource
class is the main entry point to the programmatic resource modeling API that provides ability to programmatically extend the existing JAX-RS annotated resource classes or build new resource models that may be utilized by Jersey runtime.Have a look at the example provided by the documentation:
The following table illustrates the supported requests and provided responses for the application configured in the example above:
For additional details, check the Jersey documentation.