linux

Linux中Swagger与Swagger Codegen如何配合使用

小樊
36
2025-07-03 01:22:49
栏目: 智能运维

在Linux系统中,Swagger与Swagger Codegen可以配合使用,以自动化API文档的生成和更新。以下是它们配合使用的基本步骤:

  1. 安装Swagger Codegen
brew install swagger-codegen
wget https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/3.0.30/swagger-codegen-cli-3.0.30.jar
  1. 准备Swagger规范文件
  1. 生成客户端代码
java -jar swagger-codegen-cli-3.0.30.jar generate -i /path/to/swagger.json -l java -o /path/to/output/directory
  1. 集成生成的代码
<dependency>
  <groupId>io.swagger.codegen.v3</groupId>
  <artifactId>codegen</artifactId>
  <version>3.0.30</version>
</dependency>
dependencies {
  implementation 'io.swagger.codegen.v3:codegen:3.0.30'
}
  1. 配置Swagger UI
wget https://repo1.maven.org/maven2/io/swagger/swagger-ui/3.50.0/swagger-ui-bundle.js
wget https://repo1.maven.org/maven2/io/swagger/swagger-ui/3.50.0/swagger-ui-standalone-preset.css
<!DOCTYPE html>
<html>
<head>
  <title>Swagger UI</title>
  <link rel="stylesheet" type="text/css" href="swagger-ui-standalone-preset.css">
  <script src="swagger-ui-bundle.js"></script>
  <script src="swagger-ui-standalone-preset.js"></script>
</head>
<body>
  <div id="swagger-ui"></div>
  <script>
    window.onload = function() {
      const ui = SwaggerUIBundle({
        url: "http://petstore.swagger.io/v2/swagger.json",
        dom_id: '#swagger-ui',
        deepLinking: true,
        presets: [
          SwaggerUIBundle.presets.apis,
          SwaggerUIStandalonePreset
        ],
        plugins: [
          SwaggerUIBundle.plugins.DownloadUrl
        ],
        layout: "StandaloneLayout"
      });
      window.ui = ui;
    }
  </script>
</body>
</html>

url参数替换为你的Swagger规范文件的URL。

  1. 运行和测试

通过以上步骤,你可以在Linux上成功集成Swagger与Swagger Codegen,并生成和查看API文档。

0
看了该问题的人还看了