从 Yaml 文件为 Request 和 Response 对象生成 Java 类

Generating Java classes for Request and Response objects from Yaml file(从 Yaml 文件为 Request 和 Response 对象生成 Java 类)
本文介绍了从 Yaml 文件为 Request 和 Response 对象生成 Java 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我们使用 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 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending a keyboard event from java to any application (on-screen-keyboard)(将键盘事件从 java 发送到任何应用程序(屏幕键盘))
How to make JComboBox selected item not changed when scrolling through its popuplist using keyboard(使用键盘滚动其弹出列表时如何使 JComboBox 所选项目不更改)
Capturing keystrokes without focus(在没有焦点的情况下捕获击键)
How can I position a layout right above the android on-screen keyboard?(如何将布局放置在 android 屏幕键盘的正上方?)
How to check for key being held down on startup in Java(如何检查在Java中启动时按住的键)
Android - Get keyboard key press(Android - 获取键盘按键)