简体   繁体   English

在 Spring 引导中从 swagger 或 JSON 生成模型

[英]Generate Models from swagger or JSON in Spring boot

We have requirement that the DTOs should be generated from an document where we can define the fieldName & fieldType that may be Json or Yaml.我们要求 DTO 应该从可以定义字段名称和字段类型的文档中生成,这些字段名称和字段类型可能是 Json 或 Yaml。 We are going to use these DTOs in Rest controller as request dto and response dto.我们将在 Rest controller 中使用这些 DTO 作为请求 dto 和响应 dto。 So, Requirement is that we can define our request & response fields as per client requirements and configure it in some documents and that are automatically taken in code.因此,要求是我们可以根据客户要求定义我们的请求和响应字段,并在一些文档中进行配置,并在代码中自动获取。 How can I achieve this in Spring boot java?如何在 Spring 启动 java 中实现这一点?

If you have bound to use only JSON format, you can try and use Avro .如果您已经绑定只使用 JSON 格式,您可以尝试使用Avro With it you can define the schema using JSON format and later generate Java code (& some other languages too) using Maven/Gradle plugins.有了它,您可以使用 JSON 格式定义架构,然后使用 Maven/Gradle 插件生成 Java 代码(以及其他一些语言)。

If using OpenAPI/Swagger specification you can use swagger-codegen to generate server stubs in Java.如果使用 OpenAPI/Swagger 规范,您可以使用swagger- codegen 在 Java 中生成服务器存根。

If you are free to choose message format, you can also look in to other specifications like Thrift , Protocol Buffers .如果您可以自由选择消息格式,您还可以查看其他规范,例如ThriftProtocol Buffers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM