如何在Kogito Quarkus中自定义DRL规则生成的REST端点
Kogito DRL规则自定义REST端点路径解决方案
Kogito默认会将DRL规则名/规则单元名从驼峰格式转换为短横线分隔格式作为自动生成的REST端点路径,你可以通过以下三种方式自定义端点路径为/findAllStudents:
方案1:DRL文件内添加元数据配置(最简便)
直接在DRL规则的规则单元定义下加endpointPath元数据,直接覆盖默认路径生成规则:
package com.your.business.package; // 声明规则单元 unit FindStudentsUnit; // 自定义端点路径元数据 metadata endpointPath: "/findAllStudents" end rule "findStudents" when // 你的规则匹配逻辑 then // 你的规则执行逻辑 end
配置完成后重启应用,自动生成的端点路径即为http://localhost:8080/findAllStudents。
方案2:应用配置文件覆盖路径
如果不想修改DRL文件内容,可以在项目的application.properties(Spring Boot/Quarkus通用)中添加对应规则单元的路径配置:
# 配置格式:kogito.rules-unit.<规则单元名>.endpoint-path=<自定义路径> # 未显式声明规则单元时,默认规则单元名为DRL文件名首字母大写,例如student.drl对应单元名Student kogito.rules-unit.FindStudentsUnit.endpoint-path=/findAllStudents
方案3:自定义REST接口手动调用规则(灵活度最高)
如果需要额外定制接口的请求参数、响应结构、权限校验等逻辑,可以关闭自动生成端点,自己写REST接口手动触发规则执行:
import org.kie.kogito.rules.RuleUnitInstance; import org.kie.kogito.rules.RuleUnits; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class StudentRuleController { private final RuleUnits ruleUnits; // 构造注入Kogito规则单元管理器 public StudentRuleController(RuleUnits ruleUnits) { this.ruleUnits = ruleUnits; } // 完全自定义端点路径、请求响应格式 @PostMapping("/findAllStudents") public StudentQueryResult findAllStudents(@RequestBody StudentQuery requestParam) { // 初始化规则单元,传入请求参数 FindStudentsUnit ruleUnit = new FindStudentsUnit(); ruleUnit.setQueryParam(requestParam); // 执行规则 RuleUnitInstance<FindStudentsUnit> instance = ruleUnits.createInstance(ruleUnit); instance.fire(); // 返回自定义响应 return ruleUnit.getQueryResult(); } }
内容的提问来源于stack exchange,提问作者Anshu_06
相关产品推荐
相关产品推荐

