Sandeep Joseph
Sandeep Joseph

Reputation: 43

Create Spring Boot RestController dynamically

I need to create a Rest endpoint dynamically in my Spring Boot application. Instead of statically creating the class with @RestController, is there a way to instantiate and activate a Rest service at runtime? It should be possible to specify the endpoint, input parameters etc at runtime.

Are there some Groovy options too?

Thanks, Sandeep Joseph

Upvotes: 3

Views: 2940

Answers (1)

Strelok
Strelok

Reputation: 51451

I think the approach to take would be to create a custom MvcEndpoint that will handle all requests on a specific path, from there depending on your internal configuration you can process requests. It's basically just a Servlet (that's also an option). You're fully in control of the request.

public class MyEndpoint extends AbstractMvcEndpoint 
// can optionally implements ApplicationContextAware, ServletContextAware
// to inject configuration, etc.
{
    @RequestMapping("/dynamic-enpoints-prefix/**")
    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
      // here you have the request and response. Can do anything.
    }
}

Upvotes: 1

Related Questions