简体   繁体   中英

Spring boot runtime add controller?

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

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 ?

(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 ?

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

It's not posible to register new route in the Dispatcher Servlet after the load of the application context.

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. 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. If you have some more complex variations, you could create request mappings accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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