springboot2.7集成swagger

一、导入Maven依赖

Springboot2.7需用3.0版本

<dependency>
    <groupId>io.springfox</groupId>
    <!--提供了使用Swagger和OpenAPI规范创建API文档的支持。-->
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

二、编写Swagger的配置类SwaggerConfig.java

在这里插入图片描述

package com.wms.config;

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.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;


/**
 * swagger接口文档的配置
 */
@Configuration
@EnableSwagger2
public class SwaggerConfiguration implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         *  /**表示所有的请求路径都将被映射到指定的资源路径中。
         */
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/META-INF/resources/");  // 自己的配置
        registry.addResourceHandler("/doc.html")  // 配置访问路径
                .addResourceLocations("classpath:/META-INF/resources/");  // 添加Swagger的页面资源
    }

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                // 分组名称
                .groupName("模块化开发的系统接口测试文档")
                .select()
                //这里标注控制器的位置
                .apis(RequestHandlerSelectors.basePackage("com.wms.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * api信息
     * @return api对象信息
     */
    private ApiInfo apiInfo()   {
        return new ApiInfoBuilder()
                .title("模块化开发的系统接口测试文档")  // 标题
                .description("用户中心接口文档")  // 简介
                .termsOfServiceUrl("http://localhost:8090/") // Swagger API文档中的一个可选配置项,用于指定API服务的服务条款URL。
                .contact(new Contact("ssy","http://localhost:8090/","431975683@qq.com"))
                .version("1.0")
                .build();
    }
}

三、启动类添加注解@EnableWebMvc

@SpringBootApplication
@EnableWebMvc
public class WmsApplication {

    public static void main(String[] args) {
        SpringApplication.run(WmsApplication.class, args);
    }

}

四、编写控制层

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public List<User> list()
    {
        return userService.list();
    }

    @PostMapping("/testString")
    public String testString(String str)
    {
        return str+str;
    }

    @PostMapping("/testUser")
    public User testUser(User user)
    {
        return user;
    }


}

五、启动项目测试

访问 http://localhost:8090/doc.html 即可,doc.html路径在SwaggerConfig.java配置,可以更换。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RR1mlzga-1691563054837)(D:\typoraPhoto\image-20230809131338200.png)]

测试成功

在这里插入图片描述

测试成功

在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值