pom依赖
<!-- swagger2 配置 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.6</version>
</dependency>
swagger配置类
package com.javasm.config;
import com.javasm.common.http.EnumStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
//访问地址:http://localhost:8088/swagger-ui.html#
//使用com.github.xiaoymin依赖后
// http://localhost:8088/doc.html 原路径
@Configuration
@EnableSwagger2
public class Swagger2Configuration {
//api接口包扫描路径
public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.javasm";
public static final String VERSION = "1.0.0";
@Bean
public Docket createRestApi() {
//添加全局响应状态码
List<ResponseMessage> responseMessageList = new ArrayList<>();
Arrays.stream(EnumStatus.values()).forEach(enumStatus -> {
responseMessageList.add(
new ResponseMessageBuilder().code(enumStatus.getStatus()).message(enumStatus.getMessage()).responseModel(
new ModelRef(enumStatus.getMessage())).build()
);
});
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.globalResponseMessage(RequestMethod.GET,responseMessageList)
.globalResponseMessage(RequestMethod.DELETE,responseMessageList)
.globalResponseMessage(RequestMethod.POST,responseMessageList)
.globalResponseMessage(RequestMethod.PUT,responseMessageList)
.select()
.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
.paths(PathSelectors.any()) // 可以根据url路径设置哪些请求加入文档,忽略哪些请求
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring boot Api ") //设置文档的标题
.description("Spring boot Api 接口文档") // 设置文档的描述
.version(VERSION) // 设置文档的版本信息-> 1.0.0 Version information
.termsOfServiceUrl("http://www.xxx.com") //自己设置, 设置文档的License信息->1.3 License information
.build();
}
}
注解使用场景
@Api(value = "品牌模块", tags = {"品牌模块相关接口"})
@RestController
@RequestMapping("brand")
public class BrandController {
@ApiOperation(value = "分页查询品牌", notes = "分页条件查询品牌信息", httpMethod = "GET")
@GetMapping("findByPage") // BrandCriteria : key=value 参数
@ApiImplicitParams(
{
@ApiImplicitParam(
name = "brandName",
value = "品牌名称",
required = false,
dataType = "String",
paramType = "query"),
@ApiImplicitParam(
name = "currentPage",
value = "当前页",
required = true,
dataType = "Integer",
paramType = "query"
),
@ApiImplicitParam(
name = "pageSize",
value = "每页大小",
required = true,
dataType = "Integer",
paramType = "query"
)
})
public AxiosResult findByPage(@ApiIgnore BrandCriteria brandCriteria) {
PageResult<BrandVo> brandVoPageResult = brandService.findByPage(brandCriteria);
return AxiosResult.success(brandVoPageResult);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南