springboot中使用swagger

本文介绍如何在Spring Boot项目中使用Swagger2快速生成RESTful API文档,包括Maven坐标配置、Swagger配置类编写、注解使用及访问地址说明。

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

maven坐标:

<!--swagger-->
<dependency>
	<groupId>com.spring4all</groupId>
	<artifactId>swagger-spring-boot-starter</artifactId>
	<version>1.7.1.RELEASE</version>
</dependency>

配置类:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    public static final String VERSION = "1.0.0";

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))//api接口包扫描路径
                .paths(PathSelectors.any())//可以根据url路径设置哪些请求加入文档,忽略哪些请求
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SmartItApp后台接口文档")//设置文档的标题
                .description("详细接口内容请找开发人员确定")//设置文档的描述->1.Overview
                .version(VERSION)//设置文档的版本信息-> 1.1 Version information
                .build();
    }
}

使用的注解:

@Api("告警模块") //修饰类

@ApiOperation(value = "方法描述",produces = "application/json") //修饰方法

@ApiImplicitParams({  //入参描述
	@ApiImplicitParam(name="username",value = "用户名 必传",dataType = "String"),
	@ApiImplicitParam(name="password",value = "密码 必传",dataType = "String")
})

@ApiModel(value = "User",description = "用户名")  //入参实体类描述,放在类上
@ApiModelProperty(value = "请求的页大小 必传",dataType = "Integer",example = "5")  //入参字段描述,放在字段上

访问地址:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值