动态创建Spring Boot RestController

时间:2016-11-24 10:06:21

标签: rest spring-boot

我需要在Spring Boot应用程序中动态创建Rest端点。而不是使用@RestController静态创建类,有没有办法在运行时实例化和激活Rest服务?应该可以在运行时指定端点,输入参数等。

是否也有一些Groovy选项?

谢谢, Sandeep Joseph

1 个答案:

答案 0 :(得分:0)

我认为采取的方法是创建一个自定义MvcEndpoint,它将处理特定路径上的所有请求,具体取决于您可以处理请求的内部配置。它基本上只是一个Servlet(也是一个选项)。您完全可以控制请求。

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.
    }
}