Springboot integrated swagger

本文档展示了如何在Spring Boot项目中集成Swagger,通过添加Springfox依赖并配置Swagger2,创建RESTful API的交互式文档。在启动应用后,访问`http://localhost:8080/swagger-ui.html`即可查看和测试API接口。

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

Add Swagger dependency to pom

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.9.2</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.9.2</version>
		</dependency>

Add swagger config bean

@Configuration
@EnableSwagger2
public class Swagger2Configuration {

    //api接口包扫描路径
    public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.lzhenxing.myproject";
    public static final String VERSION = "1.0.0";
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
                .paths(PathSelectors.any()) // 可以根据url路径设置哪些请求加入文档,忽略哪些请求
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot practice") //设置文档的标题
                .description("springboot practice API 接口文档") // 设置文档的描述
                .version(VERSION) // 设置文档的版本信息
                .build();
    }
}

swagger test api

@Api("springboot practice API接口")
@RestController
@RequestMapping("/project")
public class SwaggerControllerTest {

    @ApiOperation(value="my test", notes="my test", produces="application/json")
    @RequestMapping(value="/myTest", method= RequestMethod.GET)
    public String myTest() {
        return "SUCCESS";
    }

}

Start springboot

after starting springboot,visit: http://localhost:8080/swagger-ui.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值