使用SpringBoot的ExceptionHandling库

时间:2017-08-31 12:29:05

标签: spring-boot

我正在使用SpringBoot来创建服务。现在进行异常处理(使用@COntrollerAdvice),我试图创建一个库,而不是在每个服务中创建相同的文件。这是我的异常处理程序

@ControllerAdvice
public class CustomExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class);

    /*
     * This handles constraint voilation exception.
     * @param exception
     */
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseBody
    public ResponseEntity<?> handleConstraintVoilationException(ConstraintViolationException exception){
        logger.info("Inside SumsExceptionHandler.handleConstraintVoilationException()");
        ValidationErrorResponse validationResponse = null;
        for(ConstraintViolation<?> constraintViolation : exception.getConstraintViolations()) {
            validationResponse = this.makeValidationResponse(constraintViolation);
        }

        return new ResponseEntity<>(validationResponse, HttpStatus.BAD_REQUEST);
    }

    /*
     * This handles MethodArgument Type Mismatch Exception.
     * @param ex
     */
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    @ResponseBody
    public ResponseEntity<?> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException exception){
        logger.info("Inside SumsExceptionHandler.handleMethodArgumentTypeMismatchException()");

        ValidationErrorResponse validationResponse = new ValidationErrorResponse();
        validationResponse.setField(exception.getName());
        validationResponse.setCode(exception.getRequiredType().getSimpleName());
        validationResponse.setMessage(exception.getValue()+" must have valid input of type "+exception.getRequiredType().getSimpleName());

        return new ResponseEntity<>(validationResponse, HttpStatus.BAD_REQUEST);
    }

    /*
     * This handles Missing Servlet Request Parameter Exception.
     * @param ex
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    public ResponseEntity<?> handleMissingServletRequestParameterException(MissingServletRequestParameterException exception){
        logger.info("Inside SumsExceptionHandler.handleMissingServletRequestParameterException()");

        ValidationErrorResponse validationResponse = new ValidationErrorResponse();
        validationResponse.setField(exception.getParameterName());
        validationResponse.setCode(exception.getParameterType());
        validationResponse.setMessage(exception.getMessage());

        return new ResponseEntity<>(validationResponse, HttpStatus.BAD_REQUEST);
    }

    private ValidationErrorResponse makeValidationResponse(ConstraintViolation<?> constraintViolation) {
        String fieldStr = constraintViolation.getPropertyPath().toString();
        String field = null;
        if(fieldStr != null) {
            String[] fieldArr = fieldStr.split("\\.");
            field = fieldArr[fieldArr.length-1];
        }
        return new ValidationErrorResponse(field,
                constraintViolation.getMessageTemplate(),
                constraintViolation.getMessage());      

    }
}

a)如何创建一个独立的库?(这也是一个springboot项目) b)如何在其他项目中使用它?

2 个答案:

答案 0 :(得分:0)

a)如何将其创建为独立库?(这也是一个springboot项目)

  • 创建具有最小依赖关系的单独项目。

b)如何在其他项目中使用它?

  • 构建该项目,通过在类路径中添加jar来将该项目用作库

根据documentation

  

Spring Boot可以轻松创建独立的生产级产品   基于Spring的应用程序,您只需运行&#34;。

答案 1 :(得分:0)

还可以使用maven生成具有所需依赖项的新程序包,然后可以在pom.xml中添加依赖项。查找有关使用Maven创建依赖项的信息。