Java Code Examples for org.springframework.web.bind.annotation.ResponseStatus#reason()
The following examples show how to use
org.springframework.web.bind.annotation.ResponseStatus#reason() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResponseStatusExceptionResolver.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Template method that handles {@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 (for example, if multipart resolution failed) * @param ex the exception that got thrown during handler execution or the * exception that has the ResponseStatus annotation if found on the cause. * @return a corresponding ModelAndView to forward to, or {@code null} * for default processing */ 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(); }
Example 2
Source File: ExceptionTranslator.java From klask-io with GNU General Public License v3.0 | 6 votes |
@ExceptionHandler(Exception.class) public ResponseEntity<ErrorDTO> processRuntimeException(Exception ex) throws Exception { BodyBuilder builder; ErrorDTO errorDTO; ResponseStatus responseStatus = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class); if (responseStatus != null) { builder = ResponseEntity.status(responseStatus.value()); errorDTO = new ErrorDTO("error." + responseStatus.value().value(), responseStatus.reason()); } else { builder = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR); errorDTO = new ErrorDTO(ErrorConstants.ERR_INTERNAL_SERVER_ERROR, "Internal server error"); } log.error("Exception in rest call", ex); errorDTO.add("error", "error", ex.getMessage()); return builder.body(errorDTO); }
Example 3
Source File: ResponseStatusExceptionResolver.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * Template method that handles {@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 (for example, if multipart resolution failed) * @param ex the exception that got thrown during handler execution or the * exception that has the ResponseStatus annotation if found on the cause. * @return a corresponding ModelAndView to forward to, or {@code null} * for default processing */ 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 (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(); }
Example 4
Source File: GenericExceptionHandlers.java From kork with Apache License 2.0 | 6 votes |
@ExceptionHandler(Exception.class) public void handleException(Exception e, HttpServletResponse response, HttpServletRequest request) throws IOException { storeException(request, response, e); ResponseStatus responseStatus = AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class); if (responseStatus != null) { HttpStatus httpStatus = responseStatus.value(); if (httpStatus.is5xxServerError()) { logger.error(httpStatus.getReasonPhrase(), e); } else if (httpStatus != HttpStatus.NOT_FOUND) { logger.error(httpStatus.getReasonPhrase() + ": " + e.toString()); } String message = e.getMessage(); if (message == null || message.trim().isEmpty()) { message = responseStatus.reason(); } response.sendError(httpStatus.value(), message); } else { logger.error("Internal Server Error", e); response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage()); } }
Example 5
Source File: HandlerMethod.java From spring-analysis-note with MIT License | 5 votes |
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(); } }
Example 6
Source File: HandlerMethod.java From java-technology-stack with MIT License | 5 votes |
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(); } }
Example 7
Source File: HandlerMethod.java From lams with GNU General Public License v2.0 | 5 votes |
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(); } }
Example 8
Source File: ServletInvocableHandlerMethod.java From spring4-understanding with Apache License 2.0 | 5 votes |
private void initResponseStatus() { ResponseStatus annotation = getMethodAnnotation(ResponseStatus.class); if (annotation != null) { this.responseStatus = annotation.code(); this.responseReason = annotation.reason(); } }
Example 9
Source File: ContextResource.java From aws-serverless-java-container with Apache License 2.0 | 5 votes |
String resolveAnnotatedExceptionReason(Exception exception) { ResponseStatus annotation = findMergedAnnotation(exception.getClass(), ResponseStatus.class); if (annotation != null && !"".equals(annotation.reason())) { return annotation.reason(); } return exception.getLocalizedMessage(); }
Example 10
Source File: ExceptionTranslator.java From gpmr with Apache License 2.0 | 5 votes |
@ExceptionHandler(Exception.class) public ResponseEntity<ErrorDTO> processRuntimeException(Exception ex) throws Exception { BodyBuilder builder; ErrorDTO errorDTO; ResponseStatus responseStatus = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class); if (responseStatus != null) { builder = ResponseEntity.status(responseStatus.value()); errorDTO = new ErrorDTO("error." + responseStatus.value().value(), responseStatus.reason()); } else { builder = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR); errorDTO = new ErrorDTO(ErrorConstants.ERR_INTERNAL_SERVER_ERROR, "Internal server error"); } return builder.body(errorDTO); }
Example 11
Source File: SpringMvcApiReader.java From swagger-maven-plugin with Apache License 2.0 | 5 votes |
private void updateResponseStatus(Operation operation, ResponseStatus responseStatus) { int code = responseStatus.value().value(); String reason = responseStatus.reason(); if (operation.getResponses() != null && operation.getResponses().size() == 1) { String currentKey = operation.getResponses().keySet().iterator().next(); Response oldResponse = operation.getResponses().remove(currentKey); if (StringUtils.isNotEmpty(reason)) { oldResponse.setDescription(reason); } operation.response(code, oldResponse); } else { operation.response(code, new Response().description(reason)); } }
Example 12
Source File: AnnotationMethodHandlerExceptionResolver.java From lams with GNU General Public License v2.0 | 4 votes |
@SuppressWarnings("unchecked") private ModelAndView getModelAndView(Method handlerMethod, Object returnValue, ServletWebRequest webRequest) throws Exception { ResponseStatus responseStatus = AnnotatedElementUtils.findMergedAnnotation(handlerMethod, ResponseStatus.class); if (responseStatus != null) { HttpStatus statusCode = responseStatus.code(); String reason = responseStatus.reason(); if (!StringUtils.hasText(reason)) { webRequest.getResponse().setStatus(statusCode.value()); } else { webRequest.getResponse().sendError(statusCode.value(), reason); } } if (returnValue != null && AnnotationUtils.findAnnotation(handlerMethod, ResponseBody.class) != null) { return handleResponseBody(returnValue, webRequest); } if (returnValue instanceof ModelAndView) { return (ModelAndView) returnValue; } else if (returnValue instanceof Model) { return new ModelAndView().addAllObjects(((Model) returnValue).asMap()); } else if (returnValue instanceof Map) { return new ModelAndView().addAllObjects((Map<String, Object>) returnValue); } else if (returnValue instanceof View) { return new ModelAndView((View) returnValue); } else if (returnValue instanceof String) { return new ModelAndView((String) returnValue); } else if (returnValue == null) { return new ModelAndView(); } else { throw new IllegalArgumentException("Invalid handler method return value: " + returnValue); } }
Example 13
Source File: AnnotationMethodHandlerExceptionResolver.java From spring4-understanding with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") private ModelAndView getModelAndView(Method handlerMethod, Object returnValue, ServletWebRequest webRequest) throws Exception { ResponseStatus responseStatus = AnnotatedElementUtils.findMergedAnnotation(handlerMethod, ResponseStatus.class); if (responseStatus != null) { HttpStatus statusCode = responseStatus.code(); String reason = responseStatus.reason(); if (!StringUtils.hasText(reason)) { webRequest.getResponse().setStatus(statusCode.value()); } else { webRequest.getResponse().sendError(statusCode.value(), reason); } } if (returnValue != null && AnnotationUtils.findAnnotation(handlerMethod, ResponseBody.class) != null) { return handleResponseBody(returnValue, webRequest); } if (returnValue instanceof ModelAndView) { return (ModelAndView) returnValue; } else if (returnValue instanceof Model) { return new ModelAndView().addAllObjects(((Model) returnValue).asMap()); } else if (returnValue instanceof Map) { return new ModelAndView().addAllObjects((Map<String, Object>) returnValue); } else if (returnValue instanceof View) { return new ModelAndView((View) returnValue); } else if (returnValue instanceof String) { return new ModelAndView((String) returnValue); } else if (returnValue == null) { return new ModelAndView(); } else { throw new IllegalArgumentException("Invalid handler method return value: " + returnValue); } }
Example 14
Source File: ResponseStatusExceptionResolver.java From spring-analysis-note with MIT License | 3 votes |
/** * 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); }
Example 15
Source File: ResponseStatusExceptionResolver.java From java-technology-stack with MIT License | 3 votes |
/** * 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); }