2017-08-11 52 views

回答

0

您可以使用異常處理後這個

@RequestMapping(value = "login", method = RequestMethod.POST) 
@ResponseBody 
public LoginResponse login(@RequestBody SignInRequest request) throws EmailNotValidException { 
    if (isValidEmailAddress(request.getEmail())) throw new EmailNotValidException(); 

    return response; 
} 

,你可以寫法@ExceptionHandler註釋

@ExceptionHandler({EmailNotValidException.class,DataAccessException.class}) 
    public ResponseEntity<BaseResponse> dummyExceptionHandler() { 
     return new ResponseEntity<>(new BaseResponse("error"), HttpStatus.BAD_REQUEST); 
    } 
+0

謝謝你,這是幫助! –

+0

歡迎您。 你可以在這裏找到更多: https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc – rkovtiuk

+0

什麼是BaseResponse類? –