当前位置:   article > 正文

springboot3.2.1整合swagger_springboot3.2接入swagger

springboot3.2接入swagger

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

1.pom新增swagger依赖

  1. <dependency>
  2. <groupId>org.springdoc</groupId>
  3. <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
  4. <version>2.0.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springdoc</groupId>
  8. <artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
  9. <version>2.0.2</version>
  10. </dependency>

2.使用代码生成controller

  1. package com.zd.controller;
  2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  3. import com.zd.entity.User;
  4. import com.zd.resp.ApiResult;
  5. import com.zd.resp.ResultCode;
  6. import com.zd.service.UserService;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.web.bind.annotation.*;
  9. /**
  10. * <p>
  11. * 用户信息表
  12. * </p>
  13. *
  14. * @author zd
  15. * @since 2024-03-26
  16. */
  17. @RestController
  18. @RequestMapping("/api/v1/user")
  19. public class UserController {
  20. @Autowired
  21. private UserService service;
  22. @GetMapping
  23. public ApiResult<User> list(@RequestParam(required = false) Long current,
  24. @RequestParam(required = false) Long size) {
  25. Page<User> page = service.selectPage(current, size);
  26. return new ApiResult<User>(ResultCode.SUCCESS, page);
  27. }
  28. @GetMapping("/{id}")
  29. public ApiResult<User> getByid(@PathVariable("id") Long id) {
  30. User entity = service.getById(id);
  31. return new ApiResult<User>(ResultCode.SUCCESS, entity);
  32. }
  33. @PostMapping
  34. public ApiResult<User> add(@RequestBody User data) {
  35. service.save(data);
  36. return new ApiResult<User>(ResultCode.SUCCESS, data);
  37. }
  38. @PutMapping("/{id}")
  39. public ApiResult<User> update(@PathVariable("id") Long id, @RequestBody User data) {
  40. data.setUserId(id);
  41. service.updateById(data);
  42. return new ApiResult<User>(ResultCode.SUCCESS, data);
  43. }
  44. @DeleteMapping("/{id}")
  45. public ApiResult<User> delete(@PathVariable("id") Long id) {
  46. User entity = service.getById(id);
  47. service.removeById(id);
  48. return new ApiResult<User>(ResultCode.SUCCESS, entity);
  49. }
  50. }

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

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

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

  1. @Api@Tag
  2. @ApiIgnore@Parameter(hidden = true) or @Operation(hidden = true) or @Hidden
  3. @ApiImplicitParam@Parameter
  4. @ApiImplicitParams@Parameters
  5. @ApiModel@Schema
  6. @ApiModelProperty(hidden = true) → @Schema(accessMode = READ_ONLY)
  7. @ApiModelProperty@Schema
  8. @ApiOperation(value = "foo", notes = "bar") → @Operation(summary = "foo", description = "bar")
  9. @ApiParam@Parameter
  10. @ApiResponse(code = 404, message = "foo") → @ApiResponse(responseCode = "404", description = "foo")

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号