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

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

问题描述

我们使用 Swagger编辑器为我们的API规范创建了一个Yaml文件,其中包括基本URL,端点,请求,响应和头信息等。现在我想为这些API实现RESTful Web服务。为此,我正在考虑从这个Yaml文件生成我的Request和Response Java类,并且正在寻找某种代码生成器,最好是我可以在我的Maven项目中使用的maven插件/依赖项。我遇到了这个 rest client with swagger 讨论使用 swagger-codegen Maven插件,但这是为了生成客户端,我认为这是关于生成客户端代码以使用这些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文件时使用Java实现RESTful Web服务的最佳实践是什么(使用API​​创建的API规范) Swagger编辑器)以及可以使用的代码生成工具/插件。

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天全站免登陆