从 Yaml 文件为 Request 和 Response 对象生成 Java 类 [英] Generating Java classes for Request and Response objects from Yaml file

查看:53
本文介绍了从 Yaml 文件为 Request 和 Response 对象生成 Java 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用 Swagger 编辑器为我们的 API 规范创建了一个 Yaml 文件,其中包括基本 URL、端点、请求、响应和标头信息等.现在我想为这些 API 实现 RESTful Web 服务.为此,我正在考虑从这个 Yaml 文件生成我的请求和响应 Java 类,并且正在寻找某种代码生成器,最好是我可以在我的 Maven 项目中使用的 Maven 插件/依赖项.我遇到了这个 rest client with swagger 谈到使用 swagger-codegen Maven 插件,但这是为了生成 client 我相信它是关于生成客户端代码以使用这些 RESTful API,但是我需要生成类来用于服务实现.我将使用 Java 和 Spring 框架.

We have created a Yaml file using Swagger editor for our APIs specification which includes Base URL, endpoint, Request, Response and Header information etc.. Now I want to implement RESTful web service for these APIs. For that I am thinking of generating my Request and Response Java classes from this Yaml file and was looking for some kind of code generator, preferably a maven plugin/dependency which I could use in my Maven project. I came across this rest client with swagger which talks about using the swagger-codegen Maven plugin, but this is to generate the client which I believe is about generating the client code to consume these RESTful APIs, however my need is to generate classes to be used for service implementation. I will be using Java and Spring framework.

我的问题是,当我们拥有 Yaml 文件(使用 Swagger 编辑器创建的 API 规范)以及可以使用哪些代码生成工具/插件时,在 Java 中实现 RESTful Web 服务的最佳实践是什么.

My question is what are the best practices for implementing the RESTful web services in Java when we have Yaml file (API spec created using Swagger editor) and which code generation tools/plugins are available to be used.

刚刚遇到这个 服务器存根生成器 HOWTO,进一步研究.

Just came across this Server stub generator HOWTO, looking further into it.

推荐答案

Swagger-codegen maven 插件是一个不错的选择,但我建议你使用 jhipster 来生成你的java项目.它生成具有最新技术堆栈的项目,包括 spring 框架.您可以根据自己的情况选择 API-First development.我用过,效率很高.你已经有了 Yaml 文件.放入 src/main/resources/swagger/api.yml 并运行

Swagger-codegen maven plugin is a good option but I suggest you to use jhipster to generate your java project. It generates projects with latest tech stack including spring framework. You can select API-First development in your case. I used it and it was very efficient. You already have Yaml file. Put it in src/main/resources/swagger/api.yml and run

    ./mvnw generate-sources

将生成所有 java 代码.

All java codes will be generated.

这篇关于从 Yaml 文件为 Request 和 Response 对象生成 Java 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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