swagger的使用

本文介绍了如何在Spring Boot项目中使用Swagger2进行API文档的生成与管理。通过创建SwaggerConfig配置类,声明Docket方法,并指定basePackage来扫描需要展示的包。这样可以方便地查看和测试User实体类对应的Controller方法。

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


swagger的使用


在这里插入图片描述
可以声明
在SwaggerConfig类中可以声明多个Docket方法
每个Docket方法可以扫描一个

basePackage:指定要扫描的包
方便开发
在这里插入图片描述

package com.lishao.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
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 java.util.ArrayList;

@Configuration
@EnableOpenApi   //开启Swagger2
public class SwaggerConfig {

    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }

    @Bean
    public Docket docket(Environment environment){
        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev");
        //通过environment。acceptsProfiles判断是否处在自己的设定的环境中
        boolean b = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(b)
                .groupName("李少")
                .select()
                /**
                 * RequestHandlerSelectors,配置要扫描接口的方式
                 * basePackage:指定要扫描的包
                 * any:扫描全部
                 * none:不扫描
                 * withClassAnnotation:扫描类上的注解,参数是一个注解的反射对象
                 * withMethodAnnotation:扫描方法上的注解
                 */
                .apis(RequestHandlerSelectors.basePackage("com.lishao.controller"))
                //paths 过滤(忽略)什么路径
                //.paths(PathSelectors.ant("/lishao/**"))
                .build();
    }
    //配置swagger 信息apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("李少","http://localhost:8080/","854549294@qq.com");
        return new ApiInfo("李少的SwaggerApi日记",
                "即使再小的帆也能远航",
                "VV1.0",
                "www.baidu.com",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                 new ArrayList());
    }
}

在这里插入图片描述
就可以看到

在这里插入图片描述
所以这个 李少 能被重写

在这里插入图片描述
在这里插入图片描述
这个Model就是User的实体类 可以在swagger中查看

下面是controller类中的方法

package com.lishao.controller;

import com.lishao.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(tags = "用户相关")
@RestController
public class HelloController {
    @GetMapping("/")
    public String hello(){
        return "hello";
    }
    @PostMapping("/user")
    public User user(){
        return new User();
    }
    //ApiOperation放在方法上 不是放在类上
    @ApiOperation("Hello控制类")
    @GetMapping(value = "/hello2")
    public String hello2(@ApiParam("用户名") String username){
        return "hello"+username;
    }
    @PostMapping(value = "/postt")
    public User postt( User user){

        return user;
    }
}

在这里插入图片描述
在这里插入图片描述
就可以传递参数 进行测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值