spring boot kotlin validate问题
qiudaozhang
Posted on June 17, 2022
背景
@ApiModelProperty(value = "markdown原始内容", required = true)
@NotBlank(message = "markdown内容不能为空")
var md: String? = "",
验证不生效,需要修改一下
@ApiModelProperty(value = "markdown原始内容", required = true)
@field:NotBlank(message = "markdown内容不能为空")
var md: String? = "",
统一返回错误,可以针对不的异常类型进行捕获,比如
@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 ?: "未知错误")
}
💖 💪 🙅 🚩
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
opentelemetry OpenTelemetry Tracing on Spring Boot, Java Agent vs. Micrometer Tracing
August 8, 2024