本文介绍如何将java spring mvc应用程序的rest层转换为mendix应用程序中的已发布rest服务。 我们将使用反射在运行时检测控制器和api端点,并利用mendix平台sdk进行导入。
首先,我们扫描带有@RestController注解的Java类。对于每个控制器,我们提取其路径(用于Mendix资源名称),并遍历其公共方法。 我们使用一个映射来匹配Spring MVC注解(@GetMapping、@PostMapping、@PutMapping、@DeleteMapping)与对应的Mendix REST操作类型(GET、POST、PUT、DELETE)。
代码片段展示了如何收集控制器信息:
public static void exportRestLayerTo(String filepath) throws IOException { // … (代码略) … for (Class<?> restControllerClass : restControllerClasses) { // … (代码略) … for (Method method : restControllerClass.getDeclaredMethods()) {// … (代码略) … } // … (代码略) … } // … (代码略) …}
我们定义了用于导出和导入的Java记录类:MendixPublishedRestService、MendixPublishedRestServiceResource、MendixPublishedRestServiceOperation。 这些类分别表示Mendix中的已发布REST服务、资源和操作。
record MendixPublishedRestService( String serviceName, String path, String version, List<MendixPublishedRestServiceResource> resources) {}// … (其他记录类代码略) …
导出到JSON文件后,我们使用Mendix平台SDK的TypeScript API进行导入。 我们创建了与Java记录类对应的TypeScript接口:ImportedRestService、ImportedRestResource、ImportedRestOperation。
interface ImportedRestService { serviceName: string; path: string; version: string; resources: ImportedRestResource[];}// … (其他接口代码略) …
然后,我们使用SDK创建一个Mendix应用程序的工作副本,并通过循环遍历JSON数据创建Mendix中的已发布REST服务、资源和操作。 最后,我们使用mitToRepository方法将更改提交到Mendix应用程序。
const client = new mendixplatformclient();// … (代码略) …importedServices.forEach(importedService => { // … (代码略) …});// … (代码略) …
最后,我们为每个Mendix REST操作分配一个示例微流。 整个过程完成后,您可以在Mendix Studio Pro中验证已创建的REST服务。
未来改进方向包括改进Mendix平台SDK的文档,以及探索使用静态代码分析代替运行时分析来提高效率。 完整的代码可以在提供的GitHub仓库中找到。
以上就是将弹簧MVC应用程序转换为Mendix的详细内容,更多请关注范的资源库其它相关文章!
转载请注明:范的资源库 » 将弹簧MVC应用程序转换为Mendix