ubuntu

如何在Ubuntu上集成Swagger与Spring Boot

小樊
32
2025-11-22 13:25:30
栏目: 智能运维

在Ubuntu上集成Swagger与Spring Boot,可以按照以下步骤进行:

1. 创建Spring Boot项目

首先,你需要创建一个Spring Boot项目。你可以使用Spring Initializr来快速创建一个项目。

  1. 访问 Spring Initializr

  2. 配置项目的基本信息,如Group、Artifact、Name等。

  3. 在Dependencies中添加以下依赖:

    • Spring Web
    • Spring Boot DevTools
    • Springfox Swagger2
    • Springfox Swagger UI
  4. 点击 “Generate” 下载项目压缩包,解压后导入到你的IDE(如IntelliJ IDEA或Eclipse)中。

2. 配置Swagger

在Spring Boot项目中配置Swagger。

  1. 创建一个配置类来启用Swagger。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo")) // 替换为你的包名
                .paths(PathSelectors.any())
                .build();
    }
}

3. 添加Swagger注解

在你的Controller类中添加Swagger注解,以便生成API文档。

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
@Api(tags = "示例接口")
public class ExampleController {

    @GetMapping("/hello")
    @ApiOperation("示例接口")
    public String hello() {
        return "Hello, Swagger!";
    }
}

4. 运行Spring Boot应用

在终端中运行Spring Boot应用。

./mvnw spring-boot:run

5. 访问Swagger UI

打开浏览器,访问 http://localhost:8080/swagger-ui.html,你应该能够看到Swagger UI界面,其中包含了你定义的API文档。

6. 集成Swagger到前端(可选)

如果你希望在前端项目中集成Swagger UI,可以使用Swagger UI的静态资源。

  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.js
  1. 将这些文件放到你的前端项目的静态资源目录中(如 src/main/resources/static)。

  2. 在前端项目中创建一个HTML文件来加载Swagger UI:

<!DOCTYPE html>
<html>
<head>
    <title>Swagger UI</title>
    <link rel="stylesheet" type="text/css" href="/swagger-ui-bundle.css">
    <link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">
    <link rel="icon" type="image/png" href="/favicon-16x16.png" sizes="16x16">
    <style>
        html
        {
          box-sizing: border-box;
          overflow: -moz-scrollbars-vertical;
          overflow-y: scroll;
        }
        *,
        *:before,
        *:after
        {
          box-sizing: inherit;
        }
        body
        {
          margin:0;
          background: #fafafa;
        }
    </style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="/swagger-ui-bundle.js"></script>
<script src="/swagger-ui-standalone-preset.js"></script>
<script>
window.onload = function() {
  const ui = SwaggerUIBundle({
    url: "http://localhost:8080/v2/api-docs",
    dom_id: '#swagger-ui',
    deepLinking: true,
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout"
  });
  window.ui = ui;
}
</script>
</body>
</html>
  1. 运行前端项目,访问相应的HTML文件,你应该能够看到Swagger UI界面。

通过以上步骤,你就可以在Ubuntu上成功集成Swagger与Spring Boot,并且可以在前端项目中查看和测试API文档。

0
看了该问题的人还看了