spring boot kotlin validate问题

qiudaozhang

qiudaozhang

Posted on June 17, 2022

spring boot kotlin validate问题

背景

@ApiModelProperty(value = "markdown原始内容", required = true)
@NotBlank(message = "markdown内容不能为空")
var md: String? = "",

Enter fullscreen mode Exit fullscreen mode

验证不生效,需要修改一下

@ApiModelProperty(value = "markdown原始内容", required = true)
@field:NotBlank(message = "markdown内容不能为空")
var md: String? = "",
Enter fullscreen mode Exit fullscreen mode

统一返回错误,可以针对不的异常类型进行捕获,比如


@ExceptionHandler(BindException::class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
fun handle(e: BindException): R<Any> {
    val err = e.bindingResult.fieldErrors
    val msg = err[0].defaultMessage
    return R.fail(9005, msg ?: "未知错误")
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
qiudaozhang
qiudaozhang

Posted on June 17, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related