linux

如何在Linux上集成Swagger与Spring Boot

小樊
31
2025-07-01 00:46:21
栏目: 智能运维

在Linux上集成Swagger与Spring Boot可以通过以下步骤实现:

1. 创建Spring Boot项目

首先,你需要创建一个Spring Boot项目。你可以使用Spring Initializr(https://start.spring.io/)来快速生成项目结构。

2. 添加依赖

在你的pom.xml文件中添加Swagger和Springfox的依赖:

<dependencies>
    <!-- Spring Boot Starter Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Springfox Swagger2 -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>

    <!-- Springfox Swagger UI -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
</dependencies>

3. 配置Swagger

创建一个配置类来配置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.controller")) // 替换为你的控制器包路径
                .paths(PathSelectors.any())
                .build();
    }
}

4. 启动Spring Boot应用

在你的主应用类上添加@SpringBootApplication注解,并启动应用:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

5. 访问Swagger UI

启动应用后,你可以通过浏览器访问Swagger UI界面。默认情况下,Swagger UI的地址是:

http://localhost:8080/swagger-ui.html

6. 配置安全设置(可选)

如果你有安全设置(如Spring Security),你可能需要配置允许访问Swagger UI的规则。例如,在Spring Security配置类中添加以下内容:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/swagger-ui.html", "/webjars/**", "/swagger-resources/**").permitAll()
                .anyRequest().authenticated();
    }
}

7. 运行应用

在终端中运行以下命令启动Spring Boot应用:

mvn spring-boot:run

现在,你应该能够在浏览器中访问http://localhost:8080/swagger-ui.html来查看和测试你的API文档。

通过以上步骤,你就可以在Linux上成功集成Swagger与Spring Boot了。

0
看了该问题的人还看了