Cursor智能编程助手的MCP工具配置与开发指南
2026/11/5 9:36:43
访问Swagger文档时出现以下错误:
Illegal DefaultValue null for parameter type integer java.lang.NumberFormatException: For input string: ""Swagger在解析@RequestParam注解时,对于Long类型参数的defaultValue处理有兼容性问题,特别是在以下情况:
Long类型参数使用defaultValue
@RequestParam(defaultValue = "1") Long currentSwagger版本兼容性
将Long类型参数改为Integer类型:
// 修改前 @RequestParam(defaultValue = "1") Long current // 修改后 @RequestParam(defaultValue = "1") Integer current优点:
修改内容:
@GetMapping("/page") public R<IPage<ChatRecord>> pageList( @ApiParam(value = "当前页", example = "1") @RequestParam(defaultValue = "1") Integer current, @ApiParam(value = "每页条数", example = "10") @RequestParam(defaultValue = "10") Integer size, // ... 其他参数 ) { // 转换为Long类型传给MyBatis-Plus Page<ChatRecord> page = new Page<>(current.longValue(), size.longValue()); // ... }创建SwaggerConfig来处理类型替换:
@Configuration public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.OAS_30) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.hzys.controller")) .paths(PathSelectors.any()) .build() // 解决Long类型参数在Swagger中的显示问题 .directModelSubstitute(Long.class, String.class); } }为参数添加明确的示例值:
@ApiParam(value = "当前页", example = "1") @RequestParam(defaultValue = "1") Integer current// ✅ 推荐:使用Integer @RequestParam(defaultValue = "1") Integer current @RequestParam(defaultValue = "10") Integer size // ❌ 不推荐:使用Long(可能有Swagger兼容问题) @RequestParam(defaultValue = "1") Long current @RequestParam(defaultValue = "10") Long size@ApiParam(value = "当前页", example = "1") @RequestParam(defaultValue = "1") Integer current @ApiParam(value = "每页条数", example = "10") @RequestParam(defaultValue = "10") Integer size @ApiParam(value = "用户ID", example = "user001") @RequestParam(required = false) String userId创建通用的分页参数对象:
@Data @ApiModel("分页参数") public class PageParam { @ApiModelProperty(value = "当前页", example = "1") private Integer current = 1; @ApiModelProperty(value = "每页条数", example = "10") private Integer size = 10; public Page<T> toPage() { return new Page<>(current.longValue(), size.longValue()); } } // 在Controller中使用 @GetMapping("/page") public R<IPage<ChatRecord>> pageList(PageParam pageParam, @RequestParam(required = false) String userId) { Page<ChatRecord> page = pageParam.toPage(); // ... }重启Spring Boot应用。
访问:http://localhost:8091/swagger-ui/index.html
找到"聊天记录管理"下的"分页查询聊天记录"接口,确认:
# 测试分页接口 curl "http://localhost:8091/api/chat/record/page?current=1&size=10"如果枚举在Swagger中显示有问题,可以添加:
@ApiModelProperty(value = "消息类型", example = "1", allowableValues = "1,2,3") private Integer msgType;对于日期时间字段:
@ApiModelProperty(value = "反馈时间", example = "2026-01-16 10:00:00") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private String feedbackTime;对于文件上传:
@ApiParam(value = "上传文件", required = true) @RequestParam("file") MultipartFile fileSwaggerConfig.java- Swagger配置类
src/main/java/com/hzys/base/config/SwaggerConfig.java修改的文件
ChatRecordController.java- 修改分页参数类型确认pom.xml中有正确的Swagger依赖:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>✅ 将Long类型分页参数改为Integer类型
✅ 添加了@ApiParam的example属性
✅ 创建了SwaggerConfig配置类
✅ 提供了最佳实践建议