0

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!

2 Answers2

0

I did not understand what is the actual need to have a properties file and to create methods/controllers dynamically.

As far as I understand your question.,
You will be receiving a request in /users/1/ID
whenever you receive so, it should be forwarded to /users/ID

Why don't you forward?

Controller class:

@RequestMapping("/v1/customers")

@PostMapping("/{id}/ID")
public Response..<> customers(@PathVariable(value="id"..) String id){
   customersMain(id)
}

@PostMapping("/ID")
public Response..<> customersMain(@RequestParam(value = "theId") String theId){
   //do changes
}

So, always the call comes to /v1/customers/1/ID
and internally, it maps to /v1/customers/ID

smilyface
  • 5,021
  • 8
  • 41
  • 57
  • Thank you for the response @smilyface. The reason I need a dynamic controller is that each customer has his own URL for "users" endpoint. So each customer has his own controller. What my idea is, we can remove all these controllers, create new ones created from the data present in the properties file while also sanitizing the URL – Sherlock Holmes Jul 04 '23 at 11:08
  • Your actual need is still not clear. If you can update the question in a way with proper examples, chances are high to get more responses / correct answers @SherlockHolmes – smilyface Jul 04 '23 at 13:45
  • Also let me know why my solution willl not work for you (to understand the actual scenario) – smilyface Jul 04 '23 at 13:48
  • This is an assignment I got in class. The ask is to create a dynamic controller as I have stated before, additonally, Since there are already lots of legacy controllers present, I do not wish to edit and configure each and every one of them. My idea is to delete the controller package altogether and create "customer" specific ones dynamically. – Sherlock Holmes Jul 07 '23 at 07:27
0

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.