Example usage for org.springframework.web.servlet.mvc.method.annotation ResponseBodyAdvice supports

List of usage examples for org.springframework.web.servlet.mvc.method.annotation ResponseBodyAdvice supports

Introduction

In this page you can find the example usage for org.springframework.web.servlet.mvc.method.annotation ResponseBodyAdvice supports.

Prototype

boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType);

Source Link

Document

Whether this component supports the given controller method return type and the selected HttpMessageConverter type.

Usage

From source file:org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdviceChain.java

@SuppressWarnings("unchecked")
public <T> T invoke(T body, MethodParameter returnType, MediaType selectedContentType,
        Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
        ServerHttpResponse response) {/*from   ww  w. j  ava2s .  c  o  m*/

    if (this.advice != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Invoking ResponseBodyAdvice chain for body=" + body);
        }
        for (Object advice : this.advice) {
            if (advice instanceof ControllerAdviceBean) {
                ControllerAdviceBean adviceBean = (ControllerAdviceBean) advice;
                if (!adviceBean.isApplicableToBeanType(returnType.getContainingClass())) {
                    continue;
                }
                advice = adviceBean.resolveBean();
            }
            if (advice instanceof ResponseBodyAdvice) {
                ResponseBodyAdvice<T> typedAdvice = (ResponseBodyAdvice<T>) advice;
                if (typedAdvice.supports(returnType, selectedConverterType)) {
                    body = typedAdvice.beforeBodyWrite(body, returnType, selectedContentType,
                            selectedConverterType, request, response);
                }
            } else {
                throw new IllegalStateException("Expected ResponseBodyAdvice: " + advice);
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("After ResponseBodyAdvice chain body=" + body);
        }
    }
    return body;
}