Hello community,
We are hoping to develop a module that includes a REST endpoint as one of its components. In some cases, we’ll have more than one.
We do have the web development module installed in the environment and have been able to configure that to host the endpoint without issue. However, this approach is problematic because it requires a lot of extra configuration that we’d like to “just be there” when our module is installed, and we’d like to manage the structure via module updates.
We’ve also succeeded in creating a simple servlet and registering it via WebResourceManager.addServlet, which is accessible via http://ignitionaddress:8080/system/MyServlet.
However, we’d like to give this more structure, as there’s potential (almost certainty) that we’ll need to extend the interface out further. I’ve tried many things but haven’t been able to get Ignition to give me path mapping.
For instance, I’d like to have endpoints like the following:
http://localhost:8080/system/MyService1/api/v1/myendpoint1 (with GET, POST, etc)
http://localhost:8080/system/MyService1/api/v1/myendpoint2
http://localhost:8080/system/MyService2/api/v1/myotherendpoint
Still a little green with Java and getting to know the Ignition SDK (.NET crossover guy working here) so please consider that with your answer.
Thanks in advance.
The servlet you register will be called for requests with the full URL and query string available. You are responsible for further parsing that URL and calling the desired logic. There no reason a dynamic path fragment can’t be passed to such inner logic. (That’s basically what Perspective is doing with its page URL configuration.)
There’s a higher level RouteGroup
API available as well that is easier than using raw servlets.
In your gateway hook override mountRouteHandlers
, and use the RouteGroup object to build a route and set a handler on it. Looking to see if we have any examples or not…
edit: the Javadocs actually seem pretty good on it… example and all.
https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.1/com/inductiveautomation/ignition/gateway/dataroutes/RouteGroup.html
2 Likes
This looks promising but it’s not clear to me how to get the instance of routeGroup used in the example.
It’s an argument to the mountRouteHandlers
method you need to override.
Hah, read that, went to the JavaDocs, and completely forgot about it. Guess I need more coffee. Thanks for indulging me; I see it now and will give it a shot.
Overriding getMountPathAlias
will let you use something nicer in the URL than the module ID for your module. Not necessary to get things working, but something you’ll probably end up wanting.
2 Likes
Probably already inferred this, but now getting the desired results. Thanks!