Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未显示Spring启动验证消息_Java_Spring_Spring Boot_Validation_Kotlin - Fatal编程技术网

Java 未显示Spring启动验证消息

Java 未显示Spring启动验证消息,java,spring,spring-boot,validation,kotlin,Java,Spring,Spring Boot,Validation,Kotlin,我有一个Spring启动应用程序(版本为2.4.5)作为Kotlin项目。现在,当我输入无效的内容时,会收到一条错误消息,但不是我在注释中设置的错误消息 控制器 @PostMapping(value = ["/seatMap/save"]) fun addSeatPlan(@RequestPart @Valid data: DSeatMap, @RequestPart image: MultipartFile?, auth: AuthenticationToken

我有一个Spring启动应用程序(版本为2.4.5)作为Kotlin项目。现在,当我输入无效的内容时,会收到一条错误消息,但不是我在注释中设置的错误消息

控制器

  @PostMapping(value = ["/seatMap/save"])
    fun addSeatPlan(@RequestPart @Valid data: DSeatMap, @RequestPart image: MultipartFile?, auth: AuthenticationToken) {
        try {
            if (data.uuid == null) {
                seatService.addSeatMap(auth.organisation!!, data, image)
            } else {
                seatService.updateSeatMap(data, auth.organisation!!, image)
            }
        } catch (e: UnauthorizedException) {
            throw e
        }
    }
数据类

import java.util.*
import javax.validation.constraints.NotEmpty

data class DSeatMap(
    var uuid: UUID?,
    @field:NotEmpty(message = "name is empty")
    val name: String,
    var data: String,
    val quantity: Int,
    var settings: DSettings?
)

我的回答应该是message=“name为空”

如果我设置了属性,它会显示正确的属性,但我不想拥有所有的辅助信息,我只想输出消息

server.error.include-binding-errors=always
结果:

{
  "timestamp": "2021-04-22T19:56:30.058+00:00",
  "status": 400,
  "error": "Bad Request",
  "message": "Validation failed for object='data'. Error count: 1",
  "errors": [
    {
      "codes": [
        "NotEmpty.data.name",
        "NotEmpty.name",
        "NotEmpty.java.lang.String",
        "NotEmpty"
      ],
      "arguments": [
        {
          "codes": [
            "data.name",
            "name"
          ],
          "arguments": null,
          "defaultMessage": "name",
          "code": "name"
        }
      ],
      "defaultMessage": "name is empty",
      "objectName": "data",
      "field": "name",
      "rejectedValue": "",
      "bindingFailure": false,
      "code": "NotEmpty"
    }
  ],
  "path": "/api/dashboard/seatMap/save"
}
@Yunz我认为自从设置了
server.error.include binding errors=always
是否可以尝试将其设置为
never
,或者不定义此属性并依赖默认值(即
never

您需要设置
服务器.error.include message=always
自2.3版以来,Spring Boot在响应中隐藏消息字段,以避免泄漏敏感信息;我们可以将此属性与“始终”值一起使用以启用它


有关详细信息,请查看:

确定最终我找到了解决方案。我创建了一个全局异常处理程序,用于侦听MethodArgumentNotValidException。之后,我操作消息并设置我之前在注释中设置的验证消息

@RestControllerAdvice
class ExceptionControllerAdvice {

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException::class)
    fun handleValidationExceptions(ex: MethodArgumentNotValidException): Map<String, String?>? {
        val errors: MutableMap<String, String?> = HashMap()
        ex.bindingResult.allErrors.forEach { error: ObjectError ->
            val fieldName = (error as FieldError).field
            val errorMessage = error.getDefaultMessage()
            errors[fieldName] = errorMessage
        }
        return errors
    }

}
@RestControllerAdvice
类例外ControllerAdvice{
@ResponseStatus(HttpStatus.BAD_请求)
@ExceptionHandler(MethodArgumentNotValidException::类)
有趣的HandleValidationException(例如:MethodArgumentNotValidException):映射{
val错误:MutableMap=HashMap()
ex.bindingResult.allErrors.forEach{error:ObjectError->
val fieldName=(错误为FieldError)。字段
val errorMessage=error.getDefaultMessage()
错误[fieldName]=错误消息
}
返回错误
}
}

来源:

您是否可以设置
服务器。错误。include message=always
如果可以,我将以alnswer@ChrisMaggiulli我已经设置了此属性,但它仍然不起作用
@RestControllerAdvice
class ExceptionControllerAdvice {

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException::class)
    fun handleValidationExceptions(ex: MethodArgumentNotValidException): Map<String, String?>? {
        val errors: MutableMap<String, String?> = HashMap()
        ex.bindingResult.allErrors.forEach { error: ObjectError ->
            val fieldName = (error as FieldError).field
            val errorMessage = error.getDefaultMessage()
            errors[fieldName] = errorMessage
        }
        return errors
    }

}