Swagger:整合SpringBoot

该文章展示了如何使用Springfox库和knife4j增强工具来创建RESTfulAPI的Swagger文档。通过配置依赖、启用Swagger2、定义Docket以及在Controller中添加Swagger注解,可以自动生成详细的API接口文档,包括操作描述、请求参数等信息。

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

 依赖

        <!-- swagger依赖 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>
        <!-- swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
        </dependency>

配置文件

# knife4j增强
knife4j:
  enable: true

配置类

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.service.Tag;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .tags(new Tag("JsonPropertySerializationController", "JsonProperty相关测试"))
                .select()
                // 扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.wx.admin.controller"))
                // 显示所有
                .paths(PathSelectors.any())
            	// 只显示admin路径下的页面
                // .paths(PathSelectors.regex("/admin/.*"))
                .build();
    }

    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("管理后台")
                //创建人
                .contact(new Contact("程序员无羡", "https://blog.csdn.net/weixin_45427648?spm=1011.2266.3001.5343", "3220416593@qq.com"))
                //版本号
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }
}

controller

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/save")
    @ApiOperation(value = "新增用户", notes = "作者:程序员无羡")
    public ResultUtil<Boolean> savCompanyManage(@RequestBody UserDTO userDTO) {
        return userService.saveUser(userDTO);
    }

    @GetMapping("/getById")
    @ApiOperation(value = "根据编号查询用户", notes = "作者:程序员无羡")
    public ResultUtil<UserVO> getById(@RequestParam(value = "userId") @ApiParam(value = "用户编号", name = "userId") Integer userId) {
        return userService.getById(userId);
    }

    // @ApiImplicitParam和@ApiParam一回事
    @ApiImplicitParams(
        {
            @ApiImplicitParam(name="username",value="用户名",required=true),
            @ApiImplicitParam(name="password",value="密码",required=true)
        }
    )
    @PostMapping("/login")
    public AppResponse<UserRespVo> login(String username, String password){
        //...
    }


    /**
     * 分页数据
     */
    @ApiOperation(value = "分页查询所有门店")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path", name = "page", value = "当前页码", required = true, dataType = "Integer"),
        @ApiImplicitParam(paramType = "path", name = "pageSize", value = "分大小", required = true, dataType = "Integer"),
        @ApiImplicitParam(paramType = "query", name = "name", value = "名称", required = false, dataType = "String") })
    @GetMapping(value = "/pageList/{page}/{pageSize}")
    public PageVO<Store> findListByPage(@PathVariable int page,
                                        @PathVariable int pageSize,
                                        @RequestParam(value = "name",required = false) String name) {
        return userService.queryPageByName(page, pageSize,name);
    }
}

DTO

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(value = "用户参数")
public class UserDTO {

    @ApiModelProperty(value = "用户编号(新增时不用传)")
    private Integer id;

    // 默认非必传 request = false
    @ApiModelProperty(value = "用户名称", required = true)
    private String name;

    @ApiModelProperty(value = "手机号", required = true)
    private String mobile;
}

VO

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(value = "用户信息")
public class UserVO {

    @ApiModelProperty(value = "用户编号")
    private Integer id;

    @ApiModelProperty(value = "用户名称")
    private String name;

    @ApiModelProperty(value = "手机号")
    private String mobile;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员无羡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值