@Override @Nullable protected HttpStatus determineStatus(Throwable ex) { HttpStatus status = super.determineStatus(ex); if (status == null) { ResponseStatus ann = AnnotatedElementUtils.findMergedAnnotation(ex.getClass(), ResponseStatus.class); if (ann != null) { status = ann.code(); } } return status; }
/** * 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 HttpStatus determineHttpStatus(Throwable error) { if (error instanceof ResponseStatusException) { return ((ResponseStatusException) error).getStatus(); } ResponseStatus responseStatus = AnnotatedElementUtils .findMergedAnnotation(error.getClass(), ResponseStatus.class); if (responseStatus != null) { return responseStatus.code(); } return HttpStatus.INTERNAL_SERVER_ERROR; }
/** * 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 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(); } }
@Override public boolean test(Method method) { ResponseStatus annot = AnnotatedElementUtils.findMergedAnnotation(method, ResponseStatus.class); return annot != null && annot.code().equals(this.code); } }
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(); } }
@Override public int getStatusCode() { return status.code().value(); }
@Override public int getStatusCode() { return status.code().value(); }
@SuppressWarnings("rawtypes") @ExceptionHandler(GriffinException.class) public ResponseEntity handleGriffinExceptionOfClient( HttpServletRequest request, GriffinException e) { ResponseStatus responseStatus = AnnotationUtils.findAnnotation( e.getClass(), ResponseStatus.class); HttpStatus status = responseStatus.code(); String code = e.getMessage(); GriffinExceptionMessage message = GriffinExceptionMessage .valueOf(Integer.valueOf(code)); GriffinExceptionResponse body = new GriffinExceptionResponse( status, message, request.getRequestURI()); return new ResponseEntity<>(body, status); } }
/** * Template method that handles the {@link ResponseStatus @ResponseStatus} annotation. * <p>The default implementation sends a response error using * {@link HttpServletResponse#sendError(int)} or * {@link HttpServletResponse#sendError(int, String)} if the annotation has a * {@linkplain ResponseStatus#reason() reason} and then returns an empty ModelAndView. * @param responseStatus the 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, Object handler, Exception ex) throws Exception { int statusCode = responseStatus.code().value(); String reason = responseStatus.reason(); if (!StringUtils.hasLength(reason)) { response.sendError(statusCode); } else { String resolvedReason = (this.messageSource != null ? this.messageSource.getMessage(reason, null, reason, LocaleContextHolder.getLocale()) : reason); response.sendError(statusCode, resolvedReason); } return new ModelAndView(); }
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); } }
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 processResponseStatus() { ResponseStatus responseStatus = getAnnotation(ResponseStatus.class); if (responseStatus == null) { return; } HttpStatus code = responseStatus.value(); if (code == HttpStatus.INTERNAL_SERVER_ERROR) { try { code = responseStatus.code(); } catch (IncompleteAnnotationException e) { //fall through; 'responseStatus.code' was added in 4.2. } } if (hasStatusCode(code.value())) { return; } ResponseCode rc = new ResponseCode(this); rc.setCode(code.value()); String reason = responseStatus.reason(); if (!reason.isEmpty()) { rc.setCondition(reason); } statusCodes.add(rc); }
/** * Optionally set the response status using the information provided by {@code @ResponseStatus}. * @param methodParameter the controller method return parameter * @param exchange the server exchange being handled */ protected void updateResponseStatus(MethodParameter methodParameter, ServerWebExchange exchange) { ResponseStatus annotation = methodParameter.getMethodAnnotation(ResponseStatus.class); if (annotation != null) { annotation = AnnotationUtils.synthesizeAnnotation(annotation, methodParameter.getMethod()); exchange.getResponse().setStatusCode(annotation.code()); } }
@ExceptionHandler(value = {PrepException.class}) protected ResponseEntity<Object> handlePrepException(PrepException ex, WebRequest request) { HttpStatus status; ErrorResponse response; ResponseStatus responseStatus = AnnotatedElementUtils.findMergedAnnotation(ex.getClass(), ResponseStatus.class); if (responseStatus != null) { status = responseStatus.code(); response = new ErrorResponse(ex.getCode(), ex.getMessageKey(), ex.getMessageDetail()); } else { status = HttpStatus.INTERNAL_SERVER_ERROR; response = new ErrorResponse(ex.getCode(), MetatronException.DEFAULT_GLOBAL_MESSAGE, ex.getMessage()); } LOGGER.error("[API:{}] {} {}: {}, {}", ((ServletWebRequest) request).getRequest().getRequestURI(), response.getCode() == null ? "" : response.getCode(), response.getMessage(), response.getDetails()); return handleExceptionInternal(ex, response, new HttpHeaders(), status, request); }
@ExceptionHandler(value = {MetatronException.class}) protected ResponseEntity<Object> handleMetatronException(MetatronException ex, WebRequest request) { HttpStatus status; ErrorResponse response; ResponseStatus responseStatus = AnnotatedElementUtils.findMergedAnnotation(ex.getClass(), ResponseStatus.class); String details = ExceptionUtils.getRootCauseMessage(ex); if (responseStatus != null) { status = responseStatus.code(); response = new ErrorResponse(ex.getCode(), ex.getMessage(), details); } else { status = HttpStatus.INTERNAL_SERVER_ERROR; response = new ErrorResponse(ex.getCode(), MetatronException.DEFAULT_GLOBAL_MESSAGE, details); } LOGGER.error("[API:{}] {} {}: {}, {}", ((ServletWebRequest) request).getRequest().getRequestURI(), response.getCode() == null ? "" : response.getCode(), response.getMessage(), response.getDetails()); if(printStackTrace) { ex.printStackTrace(); } return handleExceptionInternal(ex, response, new HttpHeaders(), status, request); }