Java Code Examples for org.springframework.web.bind.annotation.ResponseStatus#code()

The following examples show how to use org.springframework.web.bind.annotation.ResponseStatus#code() . 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: SpringResponseBodyReturnParser.java    From RestDoc with Apache License 2.0 5 votes vote down vote up
@Override
protected int parseStatusCode(Method method) {
    ResponseStatus responseStatusAnno = AnnotatedElementUtils.getMergedAnnotation(method, ResponseStatus.class);
    if (responseStatusAnno != null)
    {
        responseStatusAnno = AnnotatedElementUtils.getMergedAnnotation(method.getDeclaringClass(), ResponseStatus.class);
    }
    if (responseStatusAnno != null)
    {
        return responseStatusAnno.code() != null ? responseStatusAnno.code().value() : responseStatusAnno.value().value();
    }
    return 200;
}
 
Example 2
Source File: WebFluxResponseStatusExceptionHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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;
}
 
Example 3
Source File: HandlerMethod.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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 4
Source File: WebFluxResponseStatusExceptionHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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;
}
 
Example 5
Source File: HandlerMethod.java    From java-technology-stack with MIT License 5 votes vote down vote up
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 lams with GNU General Public License v2.0 5 votes vote down vote up
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: ServletInvocableHandlerMethod.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void initResponseStatus() {
	ResponseStatus annotation = getMethodAnnotation(ResponseStatus.class);
	if (annotation != null) {
		this.responseStatus = annotation.code();
		this.responseReason = annotation.reason();
	}
}
 
Example 8
Source File: AnnotationMethodHandlerExceptionResolver.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@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 9
Source File: AnnotationMethodHandlerExceptionResolver.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@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);
	}
}