本文整理汇总了Java中org.springframework.web.bind.annotation.ExceptionHandler类的典型用法代码示例。如果您正苦于以下问题:Java ExceptionHandler类的具体用法?Java ExceptionHandler怎么用?Java ExceptionHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ExceptionHandler类属于org.springframework.web.bind.annotation包,在下文中一共展示了ExceptionHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: processException
import org.springframework.web.bind.annotation.ExceptionHandler; //导入依赖的package包/类
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorVM> processException(Exception ex) {
log.error("An unexpected error occurred: {}", ex.getMessage(), ex);
BodyBuilder builder;
ErrorVM errorVM;
ResponseStatus responseStatus = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class);
if (responseStatus != null) {
builder = ResponseEntity.status(responseStatus.value());
errorVM = new ErrorVM(ERROR_PREFIX + responseStatus.value().value(),
translate(ERROR_PREFIX + responseStatus.value().value()));
} else {
builder = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR);
errorVM = new ErrorVM(ErrorConstants.ERR_INTERNAL_SERVER_ERROR,
translate(ErrorConstants.ERR_INTERNAL_SERVER_ERROR));
}
return builder.body(errorVM);
}
示例2: exception
import org.springframework.web.bind.annotation.ExceptionHandler; //导入依赖的package包/类
@ExceptionHandler(Throwable.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ModelAndView exception(final Throwable throwable, final Model model) {
logger.error("Exception during execution of SpringSecurity application", throwable);
StringBuffer sb = new StringBuffer();
sb.append("Exception during execution of Spring Security application! ");
sb.append((throwable != null && throwable.getMessage() != null ? throwable.getMessage() : "Unknown error"));
if (throwable != null && throwable.getCause() != null) {
sb.append(" root cause: ").append(throwable.getCause());
}
model.addAttribute("error", sb.toString());
ModelAndView mav = new ModelAndView();
mav.addObject("error", sb.toString());
mav.setViewName("error");
return mav;
}
示例3: exception
import org.springframework.web.bind.annotation.ExceptionHandler; //导入依赖的package包/类
@ExceptionHandler(Throwable.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ModelAndView exception(final Throwable throwable, final Model model) {
logger.error("Exception during execution of SpringSecurity application", throwable);
StringBuffer sb = new StringBuffer();
sb.append("Exception during execution of Spring Security application! ");
sb.append((throwable != null && throwable.getMessage() != null ? throwable.getMessage() : "Unknown error"));
if (throwable != null && throwable.getCause() != null) {
sb.append("\n\nroot cause: ").append(throwable.getCause());
}
model.addAttribute("error", sb.toString());
ModelAndView mav = new ModelAndView();
mav.addObject("error", sb.toString());
mav.setViewName("error");
return mav;
}
示例4: processHandler
import org.springframework.web.bind.annotation.ExceptionHandler; //导入依赖的package包/类
/**
* Rest handler for validation errors.
* @param ex handled exception
* @return rest result
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<?> processHandler(MethodArgumentNotValidException ex) {
BindingResult bindingResult = ex.getBindingResult();
List<FieldError> fieldErrors = bindingResult.getFieldErrors();
List<FieldErrorDto> fieldErrorDtos = fieldErrors.stream()
.map(FieldErrorDto::new)
.collect(Collectors.toList());
ValidationResultDto validationResultDto = new ValidationResultDto();
validationResultDto.setFieldErrors(fieldErrorDtos);
LOGGER.error("VALIDATION ERROR: " + ex.getMessage());
return ResponseEntity.badRequest().body(validationResultDto);
}
示例5: exception
import org.springframework.web.bind.annotation.ExceptionHandler; //导入依赖的package包/类
@ExceptionHandler(Throwable.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ModelAndView exception(final Throwable throwable, final Model model) {
logger.error("Exception during execution of SpringSecurity application", throwable);
StringBuffer sb = new StringBuffer();
sb.append("Exception during execution of Spring Security application! ");
sb.append((throwable != null && throwable.getMessage() != null ? throwable.getMessage() : "Unknown error"));
sb.append(", root cause: ").append((throwable != null && throwable.getCause() != null ? throwable.getCause() : "Unknown cause"));
model.addAttribute("error", sb.toString());
ModelAndView mav = new ModelAndView();
mav.addObject("error", sb.toString());
mav.setViewName("error");
return mav;
}
示例6: handleMessageException
import org.springframework.web.bind.annotation.ExceptionHandler; //导入依赖的package包/类
/**
* Handles MessageException exception.
*
* @param exception the MessageException instance
* @param request the WebRequest caused exception
* @return the ResponseEntity object instance
*/
@ExceptionHandler(MessageException.class)
protected ResponseEntity<Object> handleMessageException(MessageException exception, WebRequest request) {
HttpStatus status;
switch (exception.getErrorType()) {
case NOT_FOUND:
status = HttpStatus.NOT_FOUND;
break;
case DATA_INVALID:
case PARAMETERS_INVALID:
status = HttpStatus.BAD_REQUEST;
break;
case ALREADY_EXISTS:
status = HttpStatus.CONFLICT;
break;
case AUTH_FAILED:
status = HttpStatus.UNAUTHORIZED;
break;
case OPERATION_TIMED_OUT:
case INTERNAL_ERROR:
default:
status = HttpStatus.INTERNAL_SERVER_ERROR;
break;
}
MessageError error = new MessageError(request.getHeader(CORRELATION_ID), exception.getTimestamp(),
status.value(), status.getReasonPhrase(),
exception.getMessage(), exception.getClass().getSimpleName());
return super.handleExceptionInternal(exception, error, new HttpHeaders(), status, request);
}
示例7: processRuntimeException
import org.springframework.web.bind.annotation.ExceptionHandler; //导入依赖的package包/类
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorVM> processRuntimeException(Exception ex) {
log.debug("Processing runtime exception", ex);
BodyBuilder builder;
ErrorVM errorVM;
ResponseStatus responseStatus = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class);
if (responseStatus != null) {
builder = ResponseEntity.status(responseStatus.value());
errorVM = new ErrorVM("error." + responseStatus.value().value(), responseStatus.reason());
} else {
builder = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR);
errorVM = new ErrorVM(ErrorConstants.ERR_INTERNAL_SERVER_ERROR, "Internal server error");
}
return builder.body(errorVM);
}
示例8: resolveException
import org.springframework.web.bind.annotation.ExceptionHandler; //导入依赖的package包/类
/**
* Resolve exception.
*
* @param request the request
* @param response the response
* @param ex the exception
* @return the model and view
* @throws IOException the iO exception
*/
@ExceptionHandler
public ModelAndView resolveException(final HttpServletRequest request, final HttpServletResponse response,
final Exception ex) throws IOException {
LOGGER.error(ex.getMessage(), ex);
final String contentType = request.getHeader(AJAX_REQUEST_HEADER_NAME);
if (contentType != null && contentType.equals(AJAX_REQUEST_HEADER_VALUE)) {
LOGGER.debug("Handling exception [{}] for ajax request indicated by header [{}]",
ex.getClass().getName(), AJAX_REQUEST_HEADER_NAME);
JsonUtils.renderException(ex, response);
return null;
}
LOGGER.trace("Unable to resolve exception [{}] for request. AJAX request header [{}] not found.",
ex.getClass().getName(), AJAX_REQUEST_HEADER_NAME);
final ModelAndView mv = new ModelAndView("error");
mv.addObject(ex);
return mv;
}
示例9: defaultErrorHandler
import org.springframework.web.bind.annotation.ExceptionHandler; //导入依赖的package包/类
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
logger.error("[URL] : {}", req.getRequestURL(), e);
// If the exception is annotated with @ResponseStatus rethrow it and let
// the framework handle it - like the OrderNotFoundException example
// at the start of this post.
// AnnotationUtils is a Spring Framework utility class.
if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null)
throw e;
// Otherwise setup and send the user to a default error-view.
ModelAndView mav = new ModelAndView();
mav.addObject("exception", e);
mav.addObject("url", req.getRequestURL());
mav.setViewName(DEFAULT_ERROR_VIEW);
return mav;
}
示例10: processValidationError
import org.springframework.web.bind.annotation.ExceptionHandler; //导入依赖的package包/类
/**
* Method binding issues (raised by Spring framework) - mapped to BAD_REQUEST.
*
* @param ex
* @return
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public RestErrorResponse processValidationError(MethodArgumentNotValidException ex) {
final BindingResult result = ex.getBindingResult();
final Map<String, Object> globalErrorsMap = new LinkedHashMap<>();
final List<ObjectError> globalErrors = result.getGlobalErrors();
for (ObjectError objectError : globalErrors) {
globalErrorsMap.put(objectError.getObjectName(), objectError.getDefaultMessage());
}
final List<FieldError> fieldErrors = result.getFieldErrors();
final Map<String, Object> fieldErrorsMap = new LinkedHashMap<>();
for (FieldError fieldError : fieldErrors) {
fieldErrorsMap.put(fieldError.getObjectName() + '.' + fieldError.getField(),
fieldError.getDefaultMessage());
}
final Map<String, Object> additionalDetails = new LinkedHashMap<>();
if (!globalErrorsMap.isEmpty()) {
additionalDetails.put("global-errors", globalErrorsMap);
}
if (!fieldErrorsMap.isEmpty()) {
additionalDetails.put("field-errors", fieldErrorsMap);
}
final RestErrorResponse errorResponse =
new RestErrorResponse(HttpStatus.BAD_REQUEST.value(), "Failed to validate request");
if (!additionalDetails.isEmpty()) {
errorResponse.setAdditionalDetails(additionalDetails);
}
return errorResponse;
}
示例11: handlePackageDeleteException
import org.springframework.web.bind.annotation.ExceptionHandler; //导入依赖的package包/类
@ExceptionHandler(PackageDeleteException.class)
public ResponseEntity<Map<String, String>> handlePackageDeleteException(PackageDeleteException error) {
// TODO investigate why SkipperErrorAttributes is not being invoked.
Map<String, String> map = new HashMap<>();
map.put("exception", error.getClass().getName());
map.put("message", error.getMessage());
return new ResponseEntity<Map<String, String>>(map, HttpStatus.CONFLICT);
}
示例12: processAnyException
import org.springframework.web.bind.annotation.ExceptionHandler; //导入依赖的package包/类
/**
* Catch-all exception handler method - mapped to INTERNAL_SERVER_ERROR.
*
* @param ex
* @return
*/
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public RestErrorResponse processAnyException(HttpServletRequest req, Exception ex) {
logger.error("{} {} - Unexpected error!", req.getMethod(), req.getRequestURI(), ex);
final Map<String, Object> additionalDetails = new HashMap<>();
additionalDetails.put("exception_type", ex.getClass().getName());
return new RestErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage(), additionalDetails);
}
示例13: exceptionHandler
import org.springframework.web.bind.annotation.ExceptionHandler; //导入依赖的package包/类
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseEntity<ErrorResponse> exceptionHandler(HttpServletRequest request, Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
.contentType(MediaType.APPLICATION_JSON)
.body(new ErrorResponse(e.getMessage()));
}
开发者ID:opsgenie,项目名称:playground-scenario-generator,代码行数:8,代码来源:RestResponseEntityExceptionHandler.java
示例14: exception
import org.springframework.web.bind.annotation.ExceptionHandler; //导入依赖的package包/类
@ExceptionHandler(Throwable.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String exception(final Throwable throwable, final Model model) {
logger.error("Exception during execution of SpringSecurity application", throwable);
String errorMessage = (throwable != null ? throwable.getMessage() : "Unknown error");
model.addAttribute("error", errorMessage);
return "error";
}
示例15: handleConstraintViolation
import org.springframework.web.bind.annotation.ExceptionHandler; //导入依赖的package包/类
@ExceptionHandler({ ConstraintViolationException.class })
public ResponseEntity<Object> handleConstraintViolation(final ConstraintViolationException ex, final WebRequest request) {
logger.info(ex.getClass().getName());
//
final List<String> errors = new ArrayList<String>();
for (final ConstraintViolation<?> violation : ex.getConstraintViolations()) {
errors.add(violation.getRootBeanClass().getName() + " " + violation.getPropertyPath() + ": " + violation.getMessage());
}
final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), errors);
return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
}