I have a system where there are three services. Each service has a container that holds config information and other information.
These containers also register their routes to the HTTP server. Now, I need a way to make the routes access these containers. Here is the function that registers container routes to the HTTP server.
public void initRoutes(ServletContextHandler servletContextHandler) {
ResourceConfig resourceConfig = new ResourceConfig();
// I want to register this class instance to the routes
// so, basically do something like the following:
// resourceConfig.inject(MyServiceContainer.class, this);
ServletContainer container = new ServletContainer(resourceConfig);
ServletHolder holder = new ServletHolder(container);
holder.setInitOrder(0);
holder.setInitParameter("jersey.config.server.provider.packages", "com.stackoverflow.my_service.rest");
servletContextHandler.addServlet(holder, "/v1/my_service/*");
}
Here is an example resource:
@HTTPHandlerClass
@Path("/sessions")
public class Sessions {
// And have something like the following here:
// MyServiceContainer container
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public Response createSession() {
try {
JSONObject requestBody = new JSONObject();
this.container.sendTestRequest();
return Response.ok(response.toString()).build();
} catch(UnirestException e) {
return Response.status(400).build();
}
}
}
I am using
org.glassfish.jersey -> version: 2.22
org.eclipse.jetty -> version: 9.3.9.v20160517
Everything I have read online confused me so much that I have no idea what to do anymore.