「SpringBoot」フォーム(Json)のバリデーション(validation)チェックのサンプル

サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
/**
* 引数のバリデーションチェック(jsonデータ形式)
*
* @param e 例外
* @return エラーメッセージ
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public String handleParameterException(MethodArgumentNotValidException e) {
log.error(e.getMessage(), e);
String errorMessage = CommonUtil.getErrorMessage(e.getBindingResult());
return errorMessage;
}
/**
* 引数のバリデーションチェック(formデータ形式)
*
* @param exception 例外
* @return http エラーメッセージ
*/
@ExceptionHandler(BindException.class)
public String handlerBindException(BindException exception) {
String errorMessage = CommonUtil.getErrorMessage(exception.getBindingResult());
return errorMessage;
}
}
@RestControllerAdvice @Slf4j public class GlobalExceptionHandler { /** * 引数のバリデーションチェック(jsonデータ形式) * * @param e 例外 * @return エラーメッセージ */ @ExceptionHandler(MethodArgumentNotValidException.class) public String handleParameterException(MethodArgumentNotValidException e) { log.error(e.getMessage(), e); String errorMessage = CommonUtil.getErrorMessage(e.getBindingResult()); return errorMessage; } /** * 引数のバリデーションチェック(formデータ形式) * * @param exception 例外 * @return http エラーメッセージ */ @ExceptionHandler(BindException.class) public String handlerBindException(BindException exception) { String errorMessage = CommonUtil.getErrorMessage(exception.getBindingResult()); return errorMessage; } }
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
 
    /**
     * 引数のバリデーションチェック(jsonデータ形式)
     *
     * @param e 例外
     * @return エラーメッセージ
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public String handleParameterException(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        String errorMessage = CommonUtil.getErrorMessage(e.getBindingResult());
        return errorMessage;
    }
 
    /**
     * 引数のバリデーションチェック(formデータ形式)
     *
     * @param exception 例外
     * @return http エラーメッセージ
     */
    @ExceptionHandler(BindException.class)
    public String handlerBindException(BindException exception) {
        String errorMessage = CommonUtil.getErrorMessage(exception.getBindingResult());
        return errorMessage;
    }

}

 

Spring Boot

Posted by arkgame