简体   繁体   English

Spring Boot运行时添加控制器?

[英]Spring boot runtime add controller?

Is it possible for springboot/spring-mvc or Jersey to add controller (& methods) at runtime ? springboot / spring-mvc或Jersey是否可以在运行时添加控制器(和方法)?

Meaning when running , user may input a controller name ( such as user ) , and method name sayHello , and submit , and /user/sayHello/ is enabled without restarting spring server ? 意味着在运行时,用户可以输入控制器名称(例如user ),方法名称sayHello并提交,并且/user/sayHello/是否启用而无需重新启动spring服务器?

(As to what to response , that's beyond the scope , just assume reply 'OK' ) 关于响应内容 ,这超出了范围,只需回答“确定”即可)

If spring cannot achieve it , which JVM rest framework supports such functionality ? 如果spring无法实现,那么哪个JVM rest框架支持这种功能?

It seems jersey can Programmatically build API Resource , but how about runtime ? 似乎jersey可以通过编程方式构建API资源 ,但是运行时又如何呢?

It's not posible to register new route in the Dispatcher Servlet after the load of the application context. 加载应用程序上下文之后,在Dispatcher Servlet中注册新路由是不可能的。

You can create a controller method to "catch" a variety of requests with a wildcard 您可以创建一个控制器方法来使用通配符“捕获”各种请求

@RequestMapping(value="/custom/**", method=RequestMethod.GET)
public T handle() {...}

and then route the request manually in the method body. 然后在方法正文中手动路由请求。

You could achieve this result by using URI templates in your request mapping. 您可以通过在请求映射中使用URI模板来实现此结果。 Assuming the number of variations is finite and you can abstract your request mappings to the least amount required. 假设变化的数量是有限的,那么您可以将请求映射抽象到所需的最少数量。

@RequestMapping(value="/{var1}/{var2}", method=RequestMethod.GET)
public HttpStatus handleRequest(@PathVariable String var1, @PathVariable String var2) {
    callSomeService(var1, var2);
    return HttpStatus.OK;
}

The above example would catch your "/user/sayHello" request, or any other request which contains 2 parts in the path. 上面的示例将捕获您的“ / user / sayHello”请求,或任何其他在路径中包含2部分的请求。 If you have some more complex variations, you could create request mappings accordingly. 如果您有一些更复杂的变体,则可以相应地创建请求映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM