springboot swagger

本文详细介绍如何在Spring Boot项目中集成Swagger2,包括在pom.xml中添加依赖、配置Swagger2类以及如何访问Swagger UI界面。通过这些步骤,可以方便地为API生成文档。

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

1.在pom.xml配置文件添加依赖包

<!-- Swagger2强大RESTful API文档 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>

2.Swagger2类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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;

@Configuration
@EnableSwagger2
public class Swagger2 {
	
  // 启动之后访问  http://localhost:8080/swagger-ui.html
  // Swagger2默认将所有的Controller中的RequestMapping方法都会暴露,
  // 然而在实际开发中,我们并不一定需要把所有API都提现在文档中查看,这种情况下,使用注解
  // @ApiIgnore来解决,如果应用在Controller范围上,则当前Controller中的所有方法都会被忽略,
  // 如果应用在方法上,则对应用的方法忽略暴露API

  @Bean
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
	.apis(RequestHandlerSelectors.basePackage("springboot.easyui.datagrid.controller")).paths(PathSelectors.any())
	.build();
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder().title("金含").description("IT@众汇云聚 QQ群:437393312")
	.termsOfServiceUrl("http://www.zhlkjh.com").contact("jinhan").version("1.0").build();
  }
3.访问 http://localhost:8080/swagger-ui.htm



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值