/** * Template method that handles the {@link ResponseStatus @ResponseStatus} annotation. * <p>The default implementation delegates to {@link #applyStatusAndReason} * with the status code and reason from the annotation. * @param responseStatus the {@code @ResponseStatus} annotation * @param request current HTTP request * @param response current HTTP response * @param handler the executed handler, or {@code null} if none chosen at the * time of the exception, e.g. if multipart resolution failed * @param ex the exception * @return an empty ModelAndView, i.e. exception resolved */ protected ModelAndView resolveResponseStatus(ResponseStatus responseStatus, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) throws Exception { int statusCode = responseStatus.code().value(); String reason = responseStatus.reason(); return applyStatusAndReason(statusCode, reason, response); }
/** * Template method that handles the {@link ResponseStatus @ResponseStatus} annotation. * <p>The default implementation delegates to {@link #applyStatusAndReason} * with the status code and reason from the annotation. * @param responseStatus the {@code @ResponseStatus} annotation * @param request current HTTP request * @param response current HTTP response * @param handler the executed handler, or {@code null} if none chosen at the * time of the exception, e.g. if multipart resolution failed * @param ex the exception * @return an empty ModelAndView, i.e. exception resolved */ protected ModelAndView resolveResponseStatus(ResponseStatus responseStatus, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) throws Exception { int statusCode = responseStatus.code().value(); String reason = responseStatus.reason(); return applyStatusAndReason(statusCode, reason, response); }
private String determineMessage(Throwable error) { if (error instanceof WebExchangeBindException) { return error.getMessage(); } if (error instanceof ResponseStatusException) { return ((ResponseStatusException) error).getReason(); } ResponseStatus responseStatus = AnnotatedElementUtils .findMergedAnnotation(error.getClass(), ResponseStatus.class); if (responseStatus != null) { return responseStatus.reason(); } return error.getMessage(); }
private void evaluateResponseStatus() { ResponseStatus annotation = getMethodAnnotation(ResponseStatus.class); if (annotation == null) { annotation = AnnotatedElementUtils.findMergedAnnotation(getBeanType(), ResponseStatus.class); } if (annotation != null) { this.responseStatus = annotation.code(); this.responseStatusReason = annotation.reason(); } }
private void evaluateResponseStatus() { ResponseStatus annotation = getMethodAnnotation(ResponseStatus.class); if (annotation == null) { annotation = AnnotatedElementUtils.findMergedAnnotation(getBeanType(), ResponseStatus.class); } if (annotation != null) { this.responseStatus = annotation.code(); this.responseStatusReason = annotation.reason(); } }
ResponseStatus responseStatus = findMergedAnnotation(method, ResponseStatus.class); if (responseStatus != null) { operation.response(responseStatus.value().value(), new Response().description(responseStatus.reason())); for (ResponseStatus responseStatus: errorResponses) { int code = responseStatus.code().value(); String description = defaultIfEmpty(responseStatus.reason(), responseStatus.code().getReasonPhrase()); operation.response(code, new Response().description(description));
protected ModelAndView resolveResponseStatus(ResponseStatus responseStatus, HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { int statusCode = responseStatus.value().value(); String reason = responseStatus.reason(); if (this.messageSource != null) { reason = this.messageSource.getMessage(reason, null, reason, LocaleContextHolder.getLocale()); } if (!StringUtils.hasLength(reason)) { response.sendError(statusCode); } else { response.sendError(statusCode, reason); } return new ModelAndView(); }
private ResponseEntity<Messages> handleInternal(Throwable throwable, Messages messages) { ResponseStatus responseStatus = AnnotationUtils.findAnnotation(throwable.getClass(), ResponseStatus.class); if (messages == null) { if (responseStatus != null && hasText(responseStatus.reason())) { messages = Messages.createError(responseStatus.reason(), throwable.getMessage()); } else { messages = Messages.createError(throwable.getClass().toString(), throwable.getMessage()); } } HttpStatus statusCode = Optional.ofNullable(responseStatus) .map(ResponseStatus::code) .orElse(HttpStatus.INTERNAL_SERVER_ERROR); return handleInternal(throwable, messages, statusCode); }
private String message(HandlerMethod handlerMethod) { Optional<ResponseStatus> responseStatus = fromNullable(getAnnotation(handlerMethod.getMethod(), ResponseStatus.class)); String reasonPhrase = HttpStatus.OK.getReasonPhrase(); if (responseStatus.isPresent()) { reasonPhrase = responseStatus.get().reason(); } return reasonPhrase; }
public static String message(OperationContext context) { Optional<ResponseStatus> responseStatus = context.findAnnotation(ResponseStatus.class); String reasonPhrase = HttpStatus.OK.getReasonPhrase(); if (responseStatus.isPresent()) { reasonPhrase = responseStatus.get().reason(); if (reasonPhrase.isEmpty()) { reasonPhrase = responseStatus.get().value().getReasonPhrase(); } } return reasonPhrase; }
private void evaluateResponseStatus() { ResponseStatus annotation = getMethodAnnotation(ResponseStatus.class); if (annotation == null) { annotation = AnnotatedElementUtils.findMergedAnnotation(getBeanType(), ResponseStatus.class); } if (annotation != null) { this.responseStatus = annotation.code(); this.responseStatusReason = annotation.reason(); } }
@ExceptionHandler @ResponseBody public ResponseEntity<Messages> handleException(ParametrizedMessageException e) { ResponseStatus responseStatus = AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class); if (responseStatus != null) { Messages messages = Messages.createError(responseStatus.reason(), e.getLocalizedMessage(), e.getParamsMap()); return handleInternal(e, messages, responseStatus.code()); } else { return handleException(e); } }
@ExceptionHandler(Exception.class) public ResponseEntity<Error> processRuntimeException(Exception ex) throws Exception { BodyBuilder builder; Error error; ResponseStatus responseStatus = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class); if (responseStatus != null) { builder = ResponseEntity.status(responseStatus.value()); error = new Error("error." + responseStatus.value().value(), responseStatus.reason()); } else { builder = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR); error = new Error(ErrorConstants.ERR_INTERNAL_SERVER_ERROR, "Internal server error"); } return builder.body(error); } }
private void evaluateResponseStatus() { ResponseStatus annotation = getMethodAnnotation(ResponseStatus.class); if (annotation == null) { annotation = AnnotatedElementUtils.findMergedAnnotation(getBeanType(), ResponseStatus.class); } if (annotation != null) { this.responseStatus = annotation.code(); this.responseStatusReason = annotation.reason(); } }
private ResponseEntity<Object> handleInternal(Exception ex, MultibankingError error, HttpHeaders headers, HttpStatus status, WebRequest request) { ResponseStatus responseStatus = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class); if (error == null && responseStatus!=null && responseStatus.value()!=null) error = MultibankingError.builder() .status(responseStatus.value()) .text(responseStatus.reason()).exception(ex).build(); if (error == null) error = MultibankingError.builder().status(status).exception(ex).build(); return handleExceptionInternal(ex, error, headers, status, request); }
@ExceptionHandler @ResponseBody public ResponseEntity<Object> handleException(ParametrizedMessageException e, WebRequest request) { ResponseStatus responseStatus = AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class); MultibankingError error = null; if (responseStatus != null) { error = MultibankingError.builder().status(responseStatus.value()).text(responseStatus.reason()).params(e.getParamsMap()).exception(e).build(); } else { error = MultibankingError.builder().status(HttpStatus.BAD_REQUEST).text(e.getMessage()).params(e.getParamsMap()).exception(e).build(); } return handleExceptionInternal(e, error, null, error.getStatus(), request); }
private ResponseEntity<Object> handleInternal(Exception ex, MultibankingError error, HttpHeaders headers, HttpStatus status, WebRequest request) { ResponseStatus responseStatus = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class); if (error == null && responseStatus!=null && responseStatus.value()!=null) error = MultibankingError.builder() .status(responseStatus.value()) .text(responseStatus.reason()).exception(ex).build(); if (error == null) error = MultibankingError.builder().status(status).exception(ex).build(); return handleExceptionInternal(ex, error, headers, status, request); }
@ExceptionHandler @ResponseBody public ResponseEntity<Object> handleException(ParametrizedMessageException e, WebRequest request) { ResponseStatus responseStatus = AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class); MultibankingError error = null; if (responseStatus != null) { error = MultibankingError.builder().status(responseStatus.value()).text(responseStatus.reason()).params(e.getParamsMap()).exception(e).build(); } else { error = MultibankingError.builder().status(HttpStatus.BAD_REQUEST).text(e.getMessage()).params(e.getParamsMap()).exception(e).build(); } return handleExceptionInternal(e, error, null, error.getStatus(), request); }