springspring-bootrestspring-mvcspring-restcontroller

How to create Dynamic Spring boot RestControllers from properties file


The Context

Hello, I am new to Spring and trying to work with a spring boot application which serves HTTP reqs.

I have several "customer" endpoints that have different URIs.

I want to create the rest controllers dynamically on runtime or during container start-up. I understand that I can make use of @Profile to achieve this but the need is, I need to replace these dynamic URIs with their corresponding static URIs (to sanitize their mappings recorded via wiremock which they use for mocking).

For example -

  1. I may get a request as /users/1/ID
  2. I need to sanitize this url to this "/users/ID"
  3. Each customer has a ton of such URIs
  4. Instead of manually editing and manipulating them, can I dynamically create these controllers?

I plan to create a RestController that can read data from the properties file and create the request handlers dynamically.

Format for field in properties file - <regEx_URL>=HTTP_METHOD:STATIC_URL

this should translate into something like -

@RequestMapping(path = <regEx_URL>, method = HTTP_METHOD)
private String handler(){
 // do something
 return "working!";
}

Can we achieve this?

My idea is if we can configure the Spring MVC context somehow to register these endpoints during container startup, we can serve HTTP requests.

I couldn't really find a solution on the web that suits my usecase. Any suggestions or solutions would be greatly appreciated!


Solution

  • Solved the issue with the following approach - Using JavaPoet to create Sping Beans

    I can read and process the properties file in the generator java class and create my rest controllers dynamically during compilation.