哟可以做这样的事情:
@ControllerAdvice public class ControllerExceptionTranslator { @ExceptionHandler(EntityNotFoundException.class) @ResponseStatus(HttpStatus.NOT_FOUND) @ResponseBody SimpleErrorMessage handleException(EntityNotFoundException exception){ log.debug("Entity Not Found Exception {}",exception.getMessage()); log.trace(exception.getMessage(),exception); return new SimpleErrorMessage("Entity not found","This resource was not found"); } @ExceptionHandler({UsernameNotFoundException.class}) @ResponseStatus(HttpStatus.UNAUTHORIZED) @ResponseBody SimpleErrorMessage handleException(UsernameNotFoundException exception){ log.debug("Username not found {}",exception.getLocalizedMessage()); log.trace(exception.getMessage(),exception); return new SimpleErrorMessage("Unaouthorized"," "); }}


