javax.validation.UnexpectedTypeException异常的产生与解决

javax.validation.UnexpectedTypeException异常的产生与解决

01 异常的发生场景

javax.validation.UnexpectedTypeException异常的产生与解决_第1张图片

  • 错误代码
public class MsCourseCreate {

  @ApiModelProperty(value = "课程名称",required = true)
  @NotBlank(message = "课程名称不能为空")
  private String courseName;
  @ApiModelProperty(value = "教师ID",required = true)
  @NotBlank(message = "教师ID不能为空")
  private Long teacherId;
  @ApiModelProperty(value = "班级ID",required = true)
  @NotBlank(message = "班级ID不能为空")
  private Long classId;
  @ApiModelProperty(value = "课程封面",required = true)
  @NotBlank(message = "课程封面不能为空")
  private String courseCover;
  @ApiModelProperty(value = "备注",required = true)
  @NotBlank(message = "备注不能为空")
  private String remark;
}

02 异常的产生及其原因

  • 由异常报错可知,问题出现在teacherId处
  • 通过搜索引擎的帮助,我发现这个问题是由swagger文档的参数校验产生的

03 解决方式

  • 哪里报错就检查那里的参数校验

  • 我的问题是@NotBlank不能用于修饰Long类型

  • 修改后的代码如下

public class MsCourseCreate {

  @ApiModelProperty(value = "课程名称",required = true)
  @NotBlank(message = "课程名称不能为空")
  private String courseName;
  @ApiModelProperty(value = "教师ID",required = true)
  @NotNull(message = "教师ID不能为空")
  private Long teacherId;
  @ApiModelProperty(value = "班级ID",required = true)
  @NotNull(message = "班级ID不能为空")
  private Long classId;
  @ApiModelProperty(value = "课程封面",required = true)
  @NotBlank(message = "课程封面不能为空")
  private String courseCover;
  @ApiModelProperty(value = "备注",required = true)
  @NotBlank(message = "备注不能为空")
  private String remark;
}

你可能感兴趣的:(bug,java,服务器,linux)