spring boot项目应用一:接入swagger2

本文详细介绍如何在SpringBoot项目中集成Swagger2,包括添加依赖、配置文件、启动类及控制层注解,以便生成清晰的API文档,方便开发者理解和使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.添加依赖:

		
<swaggwer.version>2.7.0</swaggwer.version>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>${swaggwer.version}</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${swaggwer.version}</version>
</dependency>

2.配置swagger文件和启动文件存放在同一级下:

package com.huayu.shiro;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


/**
 * Created by huayu on 2018/8/20.
 */
@Configuration
@EnableSwagger2
@EnableWebMvc
public class Swagger2 extends WebMvcConfigurerAdapter {
    /**
     *  有时候出现加载异常,需要添加此方法。方便找到网页位置
     *  @EnableWebMvc
     *  extends WebMvcConfigurerAdapter
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }


    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.shiro"))
                .paths(PathSelectors.any())
                .build();
    }
    //构建 api文档的详细信息函数
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("Spring Boot 测试使用 构建RESTful API")
                //版本号
                .version("1.0")
                //描述
                .description("HuaYu")
                .build();
    }

}

3.在control层添加注解方便在API文档中查看接口描述:

关于注释自己看到一个描述很全面的文章:https://blog.youkuaiyun.com/xupeng874395012/article/details/68946676

添加完注解后就可以启动项目,访问项目:IP:端口/swagger-ui.html

 

 

 

未完待续

 

Spring Cloud Gateway 2.3.12Spring Cloud 的个组件,它是个高度可配置的 HTTP 代理服务器,用于微服务架构中的API网关。要将 Swagger(通常指Swagger UI)集成到 Spring Cloud Gateway 中,你可以按照以下步骤操作: 1. 添加依赖:在你的项目中添加 SwaggerSwagger UI 的依赖。如果你使用 Maven,可以在 `pom.xml` 文件中添加: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter-web</artifactId> <version>3.x.x</version> <!-- 使用最新稳定版本 --> </dependency> <dependency> <groupId>io.github.swagger2markup</groupId> <artifactId>swagger2markup</artifactId> <version>1.0.0-M7</version> </dependency> ``` 注意:替换 `<version>` 部分为当前最稳定的 Swagger 版本。 2. 创建配置:在 `application.yml` 或 `application.properties` 中配置 Swagger 的信息,例如: ```yaml # application.yml springdoc: webFlux: openapi: info: title: 'Your API Title' version: 'v1' description: 'Description of your API' # 如果想要自定义路径和端点,可以添加这些配置 springdoc.webFlux.path=/docs, /api-docs springdoc.webFlux.endpoint-path=/v2/api-docs # Swagger UI 配置 springdoc.swagger-ui.enabled=true ``` 3. 注解控制器:在你的 API 控制器上添加 Swagger 注解,如 `@ApiOperation` 和 `@ApiResponses`。这会告诉 Swagger 生成文档的内容。 4. 自动扫描:确保你在 Spring Boot 应用中启用了自动扫描功能,以便 Swagger 能够发现并处理包含注解的类。 5. 启动应用:运行你的 Spring Boot 应用,访问 `/docs` 或者根据之前配置的路径来查看 Swagger UI 页面。 相关问题-- 1. Spring Cloud Gateway 的 Swagger 配置文件在哪里? 2. 如何在 Spring Cloud Gateway 中禁用 Swagger UI? 3. Spring Cloud Gateway 中如何设置 Swagger 的基本授权信息?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值