Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.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/5/spring-mvc/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
Spring boot 发生HttpMessageNodeReadableException时获取缺少的字段_Spring Boot_Spring Mvc_Kotlin - Fatal编程技术网

Spring boot 发生HttpMessageNodeReadableException时获取缺少的字段

Spring boot 发生HttpMessageNodeReadableException时获取缺少的字段,spring-boot,spring-mvc,kotlin,Spring Boot,Spring Mvc,Kotlin,我有一个API,它需要下面的dto结构 data class SignupRequest( @NotEmpty(message ="Username must not be empty") @NotNull(message = "Username must not be null") val username: String, @NotEmpty(message ="Password must not be em

我有一个API,它需要下面的dto结构

data class SignupRequest(
    @NotEmpty(message ="Username must not be empty")
    @NotNull(message = "Username must not be null")
    val username: String,

    @NotEmpty(message ="Password must not be empty")
    @NotNull(message = "Password must not be null")
    val password: String,

    @NotEmpty(message ="Email must not be empty")
    @NotNull(message = "Email must not be null")
    val email: String
)
下面是控制器

@RestController
class AuthenticationController {
   @Autowired
   private lateinit var userRepository: UserRepository

   @PostMapping("/signup")
   @ResponseStatus(HttpStatus.CREATED)
   fun signup(@Valid @RequestBody request: SignupRequest) : SignupResponse {
      val user = User(
            id = UUID.randomUUID(),
            username = request.username,
            password = request.password,
            email = request.email
    )

       try {
          userRepository.save(user)
          return SignupResponse(msg = "Success");
       } catch (e: Exception) {
          throw ResponseStatusException(HttpStatus.BAD_GATEWAY, "Invalid fields", e)
       }
   }
}
当我在任何一个字段丢失或为空时调用API时,我会得到
httpMessageNodeTableException
,因为默认错误消息对客户端没有多大意义,所以我尝试通过为该异常提供和错误处理程序来格式化它

@RestControllerAdvice
class ApiExceptionHandler {

   @ExceptionHandler(HttpMessageNotReadableException::class)
   fun handleMessageNotReadableException(
        ex: HttpMessageNotReadableException,
        req: HttpServletRequest,
        res: HttpServletResponse
   ) : ResponseEntity<ApiError> {
       val error = ApiError();
       return ResponseEntity<ApiError>(error, HttpStatus.BAD_REQUEST)
   }

}
@RestControllerAdvice
类ApiExceptionHandler{
@ExceptionHandler(HttpMessageTreadableException::类)
fun HandleMessageGenetradableException(
例如:HttpMessageTreadableException,
请求:HttpServletRequest,
res:HttpServletResponse
):响应性{
val error=APIRROR();
返回响应性(错误,HttpStatus.BAD_请求)
}
}
但是,我还需要知道缺少的确切字段,虽然默认错误消息包含此信息,但exception对象没有,我也无法扩展
ResponseEntityExceptionHandler
,因为这样我就必须为它处理的所有默认情况提供实现


如何轻松正确地返回API调用中缺少的字段

好问题!,我希望在2020年有一些替代方法来代替现在的手工操作(我仍然在我的应用程序上手工操作)。。2015年的问题-->好问题!,我希望在2020年有一些替代方法来代替现在的手工操作(我仍然在我的应用程序上手工操作)。。2015年的问题-->