springbootweb集成swagger

可以选择集成原生swagger,我这里选择的是在swagger之上又封装了一层的knife4j。比原生swagger更好用点,可以看接口文档,测试接口

引入依赖

复制代码
 <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>3.0.2</version>
        </dependency>

编写配置类

复制代码
```java
package com.itheima.mp.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * 配置类,注册web层相关组件
 */
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    /**
     * 通过knife4j生成接口文档
     *
     * @return
     */
    @Bean
    public Docket docket() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("swagger文档")
                .version("2.0")
                .description("这是我的文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.itheima.mp"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 设置静态资源映射
     *
     * @param registry
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

## controller层和实体类上加注解
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/1bd4feab19b3416bbbd6282782ce020a.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/5a8115016885470c93f0fef839a0badf.png)

## 访问swagger文档地址
项目启动地址/doc.html
例如 127.0.0.1:9090/doc.html
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/2bcf078ed6bd40a6b3a1cd3a3456d96f.png)
相关推荐
AAA修煤气灶刘哥5 分钟前
Agent 面试题|上下文压缩怎么兼顾 token 开销和决策效果?
人工智能·后端·面试
geovindu6 分钟前
python: Gradient Boosting
开发语言·后端·python·算法·boosting
AAA修煤气灶刘哥16 分钟前
Agent 面试题|工具调用结果不符合预期,什么时候该重规划?
人工智能·后端·面试
沉着宇宙17 分钟前
一个跑了四十轮的会话,最后靠一份日志文件续上了
后端
亦暖筑序26 分钟前
准备升级 AgentScope Java 2.0.5?这几个会话坑先避开
人工智能·后端·agent
疯华整猫29 分钟前
go vet 一行不报,单测全绿,服务却 leaking:AI 时代学 Go,重点是这三个它看不到的地方
后端·go·编程语言
不要试图纠正别人30 分钟前
同一个问题,九条 SQL 都能跑通,答案差三倍
后端·rails
夜雪劍辰30 分钟前
Spring Boot Redis 序列化踩坑:一个开关让跨工程全读成 LinkedHashMap
spring boot·redis
杨工踩坑录39 分钟前
别再手工填数据字典了:MySQL DDL + Java 实体类自动生成 Excel 登记文档
后端
wei_shuo1 小时前
SQL Server数据迁移实战:QueryMapping智能调优与主动式性能优化
javascript·后端