简体   繁体   English

使用 Spring Boot 动态创建端点 REST

[英]Dynamic REST endpoint creation with Spring Boot

I'm working on a framework that has a Spring Boot Java layer and a C++ layer that contains the core logic.我正在开发一个框架,该框架具有 Spring Boot Java 层和包含核心逻辑的 C++ 层。 The C++ layer is modular such that many services with unique responsibilities and commands share common components, including a command processor. C++ 层是模块化的,因此许多具有独特职责和命令的服务共享公共组件,包括命令处理器。 The command processor communicates with the Java layer (which is also the same for all services) via Thrift RPC.命令处理器通过 Thrift RPC 与 Java 层(所有服务也相同)进行通信。

Currently, users execute commands through a single executeCommand REST endpoint at the Java level by specifying a command name and set of parameters.目前,用户通过指定命令名称和参数集,通过 Java 级别的单个 executeCommand REST 端点执行命令。

POST http://localhost:8388/framework/executeCommand发布 http://localhost:8388/framework/executeCommand

JSON Body {"commandName": "aCommand", "parameters": {"param1": "value1", "param2": "value2"}} JSON 正文 {"commandName": "aCommand", "parameters": {"param1": "value1", "param2": "value2"}}

Is there a way to dynamically generate REST endpoints based on the set of commands provided by the C++ layer?有没有办法根据 C++ 层提供的命令集动态生成 REST 端点? Ex... each command would have an endpoint such as例如...每个命令都会有一个端点,例如

POST http://localhost:8388/framework/aCommand发布 http://localhost:8388/framework/aCommand

JSON Body {"parameters": {"param1": "value1", "param2": "value2"}} JSON 主体 {“参数”:{“param1”:“value1”,“param2”:“value2”}}

OR或者

GET http://localhost:8388/framework/aCommand?param1=value1 GET http://localhost:8388/framework/aCommand?param1=value1

Ex... at startup, the Java layer would query the C++ layer for the set of commands and other relevant information such as the appropriate REST method.例如...在启动时,Java 层将向 C++ 层查询命令集和其他相关信息,例如适当的 REST 方法。 With this information the Java layer would generate an endpoint for each command.使用此信息,Java 层将为每个命令生成一个端点。

Thanks!谢谢!

You could use a path variable in your REST controller to accept different command types, something like so:您可以在 REST controller 中使用路径变量来接受不同的命令类型,如下所示:

@RestController
@RequestMapping("/command")
@RequiredArgsConstructor
class CommandController {

    private final CommandService commandService;

    @PutMapping("/{commandType}")
    public CommandResult handleCommand(@PathVariable("commandType") String commandType,
                                       @RequestParam Map<String, String> params) {
        return commandService.execute(commandType, params);
    }
}

(Here CommandService is a service that loads available commands at startup and then validates and executes a requested command). (这里的CommandService是一种服务,它在启动时加载可用的命令,然后验证并执行请求的命令)。

You could then run a command with a request like PUT http://localhost:8080/command/someCommand?param1=value1&param2=value2然后,您可以使用PUT http://localhost:8080/command/someCommand?param1=value1&param2=value2类的请求运行命令

UPDATE更新

Some solutions for the clarified requirements (dynamic REST controller creation):澄清要求的一些解决方案(动态REST controller创建):

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

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