spring boot 和swagger2版本冲突
版本说明:
spring boot: 3.0.2
问题描述:
引入swagger2并配置好后,针对swagger2 - 2.x和3.x分别报错
1、swagger2引入2.x版本(发生了雪崩式的连锁报错)
2、swagger2引入3.x版本
1 | java.lang.TypeNotPresentException: Type javax.servlet.http.HttpServletRequest not present |
原因:
spring boot 3.x版本和swagger2 不兼容
解决办法一:
降低spring boot版本为2.x
解决办法二:(我采用的第二种)
不使用swagger2,而使用springDoc
1、引入依赖
1 2 3 4 5 | <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version> 2.1 . 0 </version> </dependency> |
2、配置文件OpenApiConfig
package com.tinz.mall.config; import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.security.SecurityRequirement; import io.swagger.v3.oas.models.security.SecurityScheme; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Arrays; @Configuration public class OpenApiConfig { @Bean public OpenAPI springShopOpenAPI() { //鉴权组件(随便起名的) SecurityScheme securityScheme = new SecurityScheme() .type(SecurityScheme.Type.HTTP) .scheme("bearer")//固定写法 .bearerFormat("JWT") .in(SecurityScheme.In.HEADER) .name("Authorization"); Components components = new Components() .addSecuritySchemes("bearer-jwt", securityScheme); //鉴权限制要求(随便起名的) SecurityRequirement securityRequirement = new SecurityRequirement() .addList("bearer-jwt", Arrays.asList("read", "write")); return new OpenAPI() // 这里的名称可以自己修改 .info(new Info().title("慕课商城 API") .description("Spring HIS application") .version("v0.0.1")) .components(components) .addSecurityItem(securityRequirement); } }
3、增加注解
(1)Controller注解 @Tag标注整个Controller @Operation标注单个请求接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | package com.tinz.mall.controller; import com.tinz.mall.common.ApiRestResponse; import com.tinz.mall.common.Constant; import com.tinz.mall.exception.MallException; import com.tinz.mall.exception.MallExceptionEnum; import com.tinz.mall.model.pojo.User; import com.tinz.mall.model.request.AddCategoryReq; import com.tinz.mall.service.CategoryService; import com.tinz.mall.service.UserService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import jakarta.servlet.http.HttpSession; import jakarta.validation.Valid; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; /** * 种类Controller */ @Tag (name = "商品种类管理" ) @RestController public class CategoryController { @Resource UserService userService; @Resource CategoryService categoryService; @Operation (summary = "addCategory" ,description = "增加商品种类" ) @PostMapping ( "/admin/category/add" ) public ApiRestResponse addCategory(HttpSession session, @Valid @RequestBody AddCategoryReq addCategoryReq){ //if(addCategoryReq.getName() == null || addCategoryReq.getType() == null || addCategoryReq.getOrderNum() == null || addCategoryReq.getParentId() == null){ // return ApiRestResponse.error(MallExceptionEnum.PARA_NOT_NULL); //} User currentUser = (User) session.getAttribute(Constant.MALL_USER); if (currentUser == null ){ return ApiRestResponse.error(MallExceptionEnum.NEED_LOGIN); } boolean adminRole = userService.checkAdminRole(currentUser); if (adminRole){ try { categoryService.add(addCategoryReq); return ApiRestResponse.success(); } catch (MallException e) { throw new RuntimeException(e); } } else { return ApiRestResponse.error(MallExceptionEnum.NEED_ADMIN); } } } |
(2)实体注解 @Schema
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | package com.tinz.mall.model.request; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.Max; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; @Schema (description = "分类" ) public class AddCategoryReq { @Schema (description = "分类目录名称,2~5个字符" ) @Size (min= 2 , max= 5 , message = "name长度2-5之间" ) @NotNull (message = "name不能为null" ) private String name; @Schema (description = "分类目录级别,最小为1,最大为3" ) @NotNull (message = "type不能为null" ) @Max (value = 3 , message = "type最大为3" ) private Integer type; @Schema (description = "父目录id" ) @NotNull (message = "parentId不能为null" ) private Integer parentId; @Schema (description = "目录展示时的排序" ) @NotNull (message = "orderNum不能为null" ) private Integer orderNum; public String getName() { return name; } public void setName(String name) { this .name = name; } public Integer getType() { return type; } public void setType(Integer type) { this .type = type; } public Integer getParentId() { return parentId; } public void setParentId(Integer parentId) { this .parentId = parentId; } public Integer getOrderNum() { return orderNum; } public void setOrderNum(Integer orderNum) { this .orderNum = orderNum; } } |
还有很多注解,可以自己查官方文档,比如
@Parameters和@Parameter 用于标识请求参数
4、启动项目(端口写自己的)
1 | http: //localhost:8082/swagger-ui/index.html |
14
收起
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧