2017-08-31 7 views
0

Ich verwende SpringBoot zum Erstellen von Diensten. Jetzt zur Ausnahmebehandlung (unter Verwendung von @COntrollerAdvice) versuche ich, eine Bibliothek zu erstellen, anstatt die gleichen Dateien in jedem Dienst zu erstellen. Das ist mein Exception HandlerExceptionHandling-Bibliothek mit SpringBoot

@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) Wie kann ich es eine Stand-alone-Bibliothek erstellen? (Dies ist auch ein springboot Project) b) Wie es dann in anderen Projekten verwenden?

Antwort

0

a) Wie kann ich ihm eine Stand-alone-Bibliothek erstellen? (Dies ist auch ein springboot Projekt)

  • erstellen separates Projekt mit minimalen Abhängigkeiten.

b) Wie kann man es dann in anderen Projekten verwenden?

  • bauen, das Projekt, das Projekt als Bibliothek verwenden von

Nach dem documentation Glas in Classpath hinzu:

Frühling Stiefel macht es einfach, Stand-alone zu schaffen, die Produktion -grade Spring-basierte Anwendungen, die Sie "einfach ausführen" können.