2016-02-12 19 views
5

Używam Wiosna Boot 1.3.x i mają następujące:Wiosna Boot @ExceptionHandler ukryj Wyjątek Nazwa

@RestController 
@RequestMapping(path = "/foo") 
public class FooController { 

    @RequestMapping(method = RequestMethod.GET, params = { "fooBar" }) 
    public Collection<Entry> firstFoo() { 
     //Do something 
    } 

    @RequestMapping(method = RequestMethod.GET, params = { "anotherFooBar" }) 
    public Collection<Entry> secondFoo() { 
     //Do something other 
    } 

} 

który działa zgodnie z oczekiwaniami. Po przejściu zły param następujące jest wyjątek:

{ 
    "error": "Bad Request", 
    "exception": "org.springframework.web.bind.UnsatisfiedServletRequestParameterException", 
    "message": "Parameter conditions \"fooBar\" OR \"anotherFooBar\" not met for actual request parameters: elementalFormulae={C11}", 
    "path": "/foo", 
    "status": 400, 
    "timestamp": 1455287433961 
} 

Potem stworzył ExceptionHandler jak widać poniżej:

@ControllerAdvice 
public class ExcptionController { 

    @ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="Invalid parameter") 
    @ExceptionHandler(UnsatisfiedServletRequestParameterException.class) 
    private void foo() { 
     //Log exception 
    } 
} 

co podnosi następujący wyjątek:

{ 
    "error": "Bad Request", 
    "exception": "org.springframework.web.bind.UnsatisfiedServletRequestParameterException", 
    "message": "Invalid parameter", 
    "path": "/api/foo", 
    "status": 400, 
    "timestamp": 1455287904886 
} 

Is It można wyłączyć pole wyjątek z reprezentacji JSON?

+0

Czy @JSONIgnore załatwi sprawę? –

Odpowiedz

7

Możesz uzyskać atrybuty błędu w swojej instrukcji kontrolera, a następnie zachować tylko Exposable Fields. Coś takiego jak:

@ControllerAdvice 
public class ExcptionController { 
    private static final List<String> EXPOSABLE_FIELDS = asList("timestamp", "status", "error", "message", "path"); 

    @Autowired private ErrorAttributes errorAttributes; 

    @ExceptionHandler(UnsatisfiedServletRequestParameterException.class) 
    private ResponseEntity foo(HttpServletRequest request) { 
     Map<String, Object> errors = getErrorAttributes(request); 
     errors.put("message", "Invalid parameter"); 

     return ResponseEntity.badRequest().body(errors); 
    } 

    private Map<String, Object> getErrorAttributes(HttpServletRequest request) { 
     ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request); 
     final boolean WITHOUT_STACK_TRACE = false; 
     Map<String, Object> attributes = errorAttributes.getErrorAttributes(requestAttributes, WITHOUT_STACK_TRACE); 

     // log exception before removing it 
     attributes.keySet().removeIf(key -> !EXPOSABLE_FIELDS.contains(key)); 

     return attributes; 
    } 
} 
+0

Ten rodzaj prac, czyli pola niewymienione w EXPOSABLE_FIELDS, są usuwane z atrybutów błędów, ale zwrócony JSON nadal wygląda tak jak powyżej: – tplacht

+0

Pole "wyjątek" nadal tam jest? –

+0

Tak, nadal jest wyświetlany, chociaż błędy nie zawierają już pola. – tplacht

3

Rozwiązałem ten styl fasoli. Dodanie następującej definicji komponentu bean do konfiguracji warstwy REST, zastępując w moim przypadku rozwiązany problem bez kodu wykonawczego podczas przetwarzania wyjątku.

@Bean 
public ErrorAttributes errorAttributes() { 
    return new DefaultErrorAttributes() { 

     @Override 
     public Map<String, Object> getErrorAttributes(
       RequestAttributes requestAttributes, 
       boolean includeStackTrace) { 

      Map<String, Object> errorAttributes 
       = super.getErrorAttributes(requestAttributes, includeStackTrace); 
      errorAttributes.remove("exception"); 
      return errorAttributes; 
     } 

    }; 
} 
Powiązane problemy