Spring Boot运行时添加控制器? [英] Spring boot runtime add controller?

查看:105
本文介绍了Spring Boot运行时添加控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

springboot/spring-mvc或Jersey是否可以在运行时处添加控制器(和方法)?

Is it possible for springboot/spring-mvc or Jersey to add controller (& methods) at runtime ?

在运行时,用户可以输入控制器名称(例如 user )和方法名称 sayHello ,并提交和/user/sayHello/启用而不重启spring服务器?

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 ?

(关于如何回应,这超出了范围,仅假设回答'OK')

(As to what to response , that's beyond the scope , just assume reply 'OK' )

如果spring无法实现,那么哪个JVM rest框架支持这种功能?

If spring cannot achieve it , which JVM rest framework supports such functionality ?

似乎球衣可以以编程方式构建API资源,但是如何呢?运行时?

It seems jersey can Programmatically build API Resource , but how about runtime ?

推荐答案

您可以通过在请求映射中使用URI模板来实现此结果.假设变化的数量是有限的,那么您可以将请求映射抽象到所需的最少数量.

You could achieve this result by using URI templates in your request mapping. 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;
}

上面的示例将捕获您的"/user/sayHello"请求,或任何其他在路径中包含2部分的请求.如果您有一些更复杂的变体,则可以相应地创建请求映射.

The above example would catch your "/user/sayHello" request, or any other request which contains 2 parts in the path. If you have some more complex variations, you could create request mappings accordingly.

这篇关于Spring Boot运行时添加控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆