I have a package for internal use that when added to a project needs to send a notification to our QA application that it is live, and at what endpoint it is available. To that end, I need to get the context root of the project.
The problem What I attempted first was the following:
@Path("/myEndpoint")
public class MyAPI {
@Context
ServletContext context
@Get
public MyObject getMyObject() {
...
context.getContextPath(); // store path in MyObject
return myObjectInstance;
}
The issue with this is that someone needs to hit the endpoint first in order to get the path info. I want something that will run when the project is deployed. My next attempt was to use @Singleton
with @PostConstruct
, but I didn't think it best practice to have my API class running that sort of code (and the ServletContext was null, so it wouldn't work even if I did opt to leave the code in...)
I've tried one or two other things, but to no avail. A suggestion in the department was to make a Maven plugin that would ping the QA server with that sort of information, but I'm hoping to find a Java solution for simplicity's sake. Is there another way to get access to the context root and have a method automatically fire after project deployment?
I am using Java8 with Maven, and our applications are deployed using Glassfish.