springboot3.2.1整合swagger

项目主体参考我的另一篇:springboot3.2.1整合mybatis-plus-generator3.5.3,并自定义vm模板_mybatis generator 自定义模板-CSDN博客

1.pom新增swagger依赖

        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
            <version>2.0.2</version>
        </dependency>

2.使用代码生成controller

package com.zd.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zd.entity.User;
import com.zd.resp.ApiResult;
import com.zd.resp.ResultCode;
import com.zd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户信息表
 * </p>
 *
 * @author zd
 * @since 2024-03-26
 */
@RestController
@RequestMapping("/api/v1/user")
public class UserController {

    @Autowired
    private UserService service;

    @GetMapping
    public ApiResult<User> list(@RequestParam(required = false) Long current,
                                @RequestParam(required = false) Long size) {
        Page<User> page = service.selectPage(current, size);
        return new ApiResult<User>(ResultCode.SUCCESS, page);
    }

    @GetMapping("/{id}")
    public ApiResult<User> getByid(@PathVariable("id") Long id) {
        User entity = service.getById(id);
        return new ApiResult<User>(ResultCode.SUCCESS, entity);
    }

    @PostMapping
    public ApiResult<User> add(@RequestBody User data) {
        service.save(data);
        return new ApiResult<User>(ResultCode.SUCCESS, data);
    }

    @PutMapping("/{id}")
    public ApiResult<User> update(@PathVariable("id") Long id, @RequestBody User data) {
        data.setUserId(id);
        service.updateById(data);
        return new ApiResult<User>(ResultCode.SUCCESS, data);
    }

    @DeleteMapping("/{id}")
    public ApiResult<User> delete(@PathVariable("id") Long id) {
        User entity = service.getById(id);
        service.removeById(id);
        return new ApiResult<User>(ResultCode.SUCCESS, entity);
    }


}

3.启动项目,访问:http://ip:port/swagger-ui.html

4.最后,swagger中2版本和3版本用法对比

从Springfox迁移过来的,需要修改注解:

@Api → @Tag
@ApiIgnore → @Parameter(hidden = true) or @Operation(hidden = true) or @Hidden
@ApiImplicitParam → @Parameter
@ApiImplicitParams → @Parameters
@ApiModel → @Schema
@ApiModelProperty(hidden = true) → @Schema(accessMode = READ_ONLY)
@ApiModelProperty → @Schema
@ApiOperation(value = "foo", notes = "bar") → @Operation(summary = "foo", description = "bar")
@ApiParam → @Parameter
@ApiResponse(code = 404, message = "foo") → @ApiResponse(responseCode = "404", description = "foo")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值