Java Code Examples for org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice

The following examples show how to use org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice. These examples are extracted from open source projects. 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
/**
 * A method available to subclasses for adding default {@link HandlerExceptionResolver}s.
 * <p>Adds the following exception resolvers:
 * <ul>
 * <li>{@link ExceptionHandlerExceptionResolver}
 * for handling exceptions through @{@link ExceptionHandler} methods.
 * <li>{@link ResponseStatusExceptionResolver}
 * for exceptions annotated with @{@link ResponseStatus}.
 * <li>{@link DefaultHandlerExceptionResolver}
 * for resolving known Spring exception types
 * </ul>
 */
protected final void addDefaultHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
	ExceptionHandlerExceptionResolver exceptionHandlerResolver = createExceptionHandlerExceptionResolver();
	exceptionHandlerResolver.setContentNegotiationManager(mvcContentNegotiationManager());
	exceptionHandlerResolver.setMessageConverters(getMessageConverters());
	exceptionHandlerResolver.setCustomArgumentResolvers(getArgumentResolvers());
	exceptionHandlerResolver.setCustomReturnValueHandlers(getReturnValueHandlers());
	if (jackson2Present) {
		exceptionHandlerResolver.setResponseBodyAdvice(
				Collections.<ResponseBodyAdvice<?>>singletonList(new JsonViewResponseBodyAdvice()));
	}
	exceptionHandlerResolver.setApplicationContext(this.applicationContext);
	exceptionHandlerResolver.afterPropertiesSet();
	exceptionResolvers.add(exceptionHandlerResolver);

	ResponseStatusExceptionResolver responseStatusResolver = new ResponseStatusExceptionResolver();
	responseStatusResolver.setMessageSource(this.applicationContext);
	exceptionResolvers.add(responseStatusResolver);

	exceptionResolvers.add(new DefaultHandlerExceptionResolver());
}
 
Example 2
/**
 * A method available to subclasses for adding default {@link HandlerExceptionResolver}s.
 * <p>Adds the following exception resolvers:
 * <ul>
 * <li>{@link ExceptionHandlerExceptionResolver}
 * for handling exceptions through @{@link ExceptionHandler} methods.
 * <li>{@link ResponseStatusExceptionResolver}
 * for exceptions annotated with @{@link ResponseStatus}.
 * <li>{@link DefaultHandlerExceptionResolver}
 * for resolving known Spring exception types
 * </ul>
 */
protected final void addDefaultHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
	ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver = new ExceptionHandlerExceptionResolver();
	exceptionHandlerExceptionResolver.setContentNegotiationManager(mvcContentNegotiationManager());
	exceptionHandlerExceptionResolver.setMessageConverters(getMessageConverters());
	if (jackson2Present) {
		List<ResponseBodyAdvice<?>> interceptors = new ArrayList<ResponseBodyAdvice<?>>();
		interceptors.add(new JsonViewResponseBodyAdvice());
		exceptionHandlerExceptionResolver.setResponseBodyAdvice(interceptors);
	}
	exceptionHandlerExceptionResolver.setApplicationContext(this.applicationContext);
	exceptionHandlerExceptionResolver.afterPropertiesSet();
	exceptionResolvers.add(exceptionHandlerExceptionResolver);

	ResponseStatusExceptionResolver responseStatusExceptionResolver = new ResponseStatusExceptionResolver();
	responseStatusExceptionResolver.setMessageSource(this.applicationContext);
	exceptionResolvers.add(responseStatusExceptionResolver);

	exceptionResolvers.add(new DefaultHandlerExceptionResolver());
}
 
Example 3
@SuppressWarnings("unchecked")
private void verifyResponseBodyAdvice(Object bean) {
	assertNotNull(bean);
	Object value = new DirectFieldAccessor(bean).getPropertyValue("responseBodyAdvice");
	assertNotNull(value);
	assertTrue(value instanceof List);
	List<ResponseBodyAdvice<?>> converters = (List<ResponseBodyAdvice<?>>) value;
	assertTrue(converters.get(0) instanceof JsonViewResponseBodyAdvice);
}
 
Example 4
@SuppressWarnings("unchecked")
private void verifyRequestResponseBodyAdvice(Object bean) {
	assertNotNull(bean);
	Object value = new DirectFieldAccessor(bean).getPropertyValue("requestResponseBodyAdvice");
	assertNotNull(value);
	assertTrue(value instanceof List);
	List<ResponseBodyAdvice<?>> converters = (List<ResponseBodyAdvice<?>>) value;
	assertTrue(converters.get(0) instanceof JsonViewRequestBodyAdvice);
	assertTrue(converters.get(1) instanceof JsonViewResponseBodyAdvice);
}
 
Example 5
Source Project: Milkomeda   Source File: SpringMvcPolyfill.java    License: MIT License 5 votes vote down vote up
/**
 * 动态添加消息体响应切面
 * @param returnValueHandlers       响应处理器列表(需要注入 {@link org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#getReturnValueHandlers()})
 * @param handlerExceptionResolver  异常处理解析器(需要注入 {@link org.springframework.web.servlet.HandlerExceptionResolver})
 * @param responseBodyAdvice        ResponseBodyAdvice
 */
public static void addDynamicResponseBodyAdvice(List<HandlerMethodReturnValueHandler> returnValueHandlers, HandlerExceptionResolver handlerExceptionResolver, ResponseBodyAdvice<?> responseBodyAdvice) {
    if (CollectionUtils.isEmpty(returnValueHandlers)) {
        return;
    }
    // 下面这行添加不起作用,由于内部构建已经完成
    // adapter.setResponseBodyAdvice(Collections.singletonList(responseBodyAdvice));
    for (HandlerMethodReturnValueHandler returnValueHandler : returnValueHandlers) {
        // 只有AbstractMessageConverterMethodArgumentResolver继承类型(主要是HttpEntityMethodProcessor、RequestResponseBodyMethodProcessor)有Advice Chain
        if (returnValueHandler instanceof AbstractMessageConverterMethodArgumentResolver) {
            // TODO <mark> 由于使用底层API, 这个advice.responseBodyAdvice属性在未来版本中可能会改
            List<Object> advices = ReflectUtil.invokeFieldPath(returnValueHandler, "advice.responseBodyAdvice");
            if (CollectionUtils.isEmpty(advices)) {
                continue;
            }
            advices.add(responseBodyAdvice);
        }
    }

    // 动态添加到异常处理(因为源码流程中的异常处理是新加载的HandlerExceptionResolver,与正常响应处理不是同个处理集)
    if (handlerExceptionResolver instanceof HandlerExceptionResolverComposite) {
        // SpringMVC默认为注册HandlerExceptionResolverComposite的Bean
        List<HandlerExceptionResolver> exceptionResolvers = ((HandlerExceptionResolverComposite) handlerExceptionResolver).getExceptionResolvers();
        if (CollectionUtils.isEmpty(exceptionResolvers)) {
            return;
        }
        for (HandlerExceptionResolver exceptionResolver : exceptionResolvers) {
            if (exceptionResolver instanceof ExceptionHandlerExceptionResolver) {
                HandlerMethodReturnValueHandlerComposite returnValueHandlerComposite = ((ExceptionHandlerExceptionResolver) exceptionResolver).getReturnValueHandlers();
                if (returnValueHandlerComposite == null) {
                    return;
                }
                SpringMvcPolyfill.addDynamicResponseBodyAdvice(returnValueHandlerComposite.getHandlers(), null, responseBodyAdvice);
            }
        }
    }
}
 
Example 6
@SuppressWarnings("unchecked")
private void verifyResponseBodyAdvice(Object bean) {
	assertNotNull(bean);
	Object value = new DirectFieldAccessor(bean).getPropertyValue("responseBodyAdvice");
	assertNotNull(value);
	assertTrue(value instanceof List);
	List<ResponseBodyAdvice<?>> converters = (List<ResponseBodyAdvice<?>>) value;
	assertTrue(converters.get(0) instanceof JsonViewResponseBodyAdvice);
}
 
Example 7
@SuppressWarnings("unchecked")
private void verifyRequestResponseBodyAdvice(Object bean) {
	assertNotNull(bean);
	Object value = new DirectFieldAccessor(bean).getPropertyValue("requestResponseBodyAdvice");
	assertNotNull(value);
	assertTrue(value instanceof List);
	List<ResponseBodyAdvice<?>> converters = (List<ResponseBodyAdvice<?>>) value;
	assertTrue(converters.get(0) instanceof JsonViewRequestBodyAdvice);
	assertTrue(converters.get(1) instanceof JsonViewResponseBodyAdvice);
}
 
Example 8
/**
 * Returns a {@link RequestMappingHandlerAdapter} for processing requests
 * through annotated controller methods. Consider overriding one of these
 * other more fine-grained methods:
 * <ul>
 * <li>{@link #addArgumentResolvers} for adding custom argument resolvers.
 * <li>{@link #addReturnValueHandlers} for adding custom return value handlers.
 * <li>{@link #configureMessageConverters} for adding custom message converters.
 * </ul>
 */
@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
	RequestMappingHandlerAdapter adapter = createRequestMappingHandlerAdapter();
	adapter.setContentNegotiationManager(mvcContentNegotiationManager());
	adapter.setMessageConverters(getMessageConverters());
	adapter.setWebBindingInitializer(getConfigurableWebBindingInitializer());
	adapter.setCustomArgumentResolvers(getArgumentResolvers());
	adapter.setCustomReturnValueHandlers(getReturnValueHandlers());

	if (jackson2Present) {
		adapter.setRequestBodyAdvice(
				Collections.<RequestBodyAdvice>singletonList(new JsonViewRequestBodyAdvice()));
		adapter.setResponseBodyAdvice(
				Collections.<ResponseBodyAdvice<?>>singletonList(new JsonViewResponseBodyAdvice()));
	}

	AsyncSupportConfigurer configurer = new AsyncSupportConfigurer();
	configureAsyncSupport(configurer);
	if (configurer.getTaskExecutor() != null) {
		adapter.setTaskExecutor(configurer.getTaskExecutor());
	}
	if (configurer.getTimeout() != null) {
		adapter.setAsyncRequestTimeout(configurer.getTimeout());
	}
	adapter.setCallableInterceptors(configurer.getCallableInterceptors());
	adapter.setDeferredResultInterceptors(configurer.getDeferredResultInterceptors());

	return adapter;
}
 
Example 9
/**
 * Returns a {@link RequestMappingHandlerAdapter} for processing requests
 * through annotated controller methods. Consider overriding one of these
 * other more fine-grained methods:
 * <ul>
 * <li>{@link #addArgumentResolvers} for adding custom argument resolvers.
 * <li>{@link #addReturnValueHandlers} for adding custom return value handlers.
 * <li>{@link #configureMessageConverters} for adding custom message converters.
 * </ul>
 */
@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
	List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<HandlerMethodArgumentResolver>();
	addArgumentResolvers(argumentResolvers);

	List<HandlerMethodReturnValueHandler> returnValueHandlers = new ArrayList<HandlerMethodReturnValueHandler>();
	addReturnValueHandlers(returnValueHandlers);

	RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
	adapter.setContentNegotiationManager(mvcContentNegotiationManager());
	adapter.setMessageConverters(getMessageConverters());
	adapter.setWebBindingInitializer(getConfigurableWebBindingInitializer());
	adapter.setCustomArgumentResolvers(argumentResolvers);
	adapter.setCustomReturnValueHandlers(returnValueHandlers);

	if (jackson2Present) {
		List<RequestBodyAdvice> requestBodyAdvices = new ArrayList<RequestBodyAdvice>();
		requestBodyAdvices.add(new JsonViewRequestBodyAdvice());
		adapter.setRequestBodyAdvice(requestBodyAdvices);

		List<ResponseBodyAdvice<?>> responseBodyAdvices = new ArrayList<ResponseBodyAdvice<?>>();
		responseBodyAdvices.add(new JsonViewResponseBodyAdvice());
		adapter.setResponseBodyAdvice(responseBodyAdvices);
	}

	AsyncSupportConfigurer configurer = new AsyncSupportConfigurer();
	configureAsyncSupport(configurer);

	if (configurer.getTaskExecutor() != null) {
		adapter.setTaskExecutor(configurer.getTaskExecutor());
	}
	if (configurer.getTimeout() != null) {
		adapter.setAsyncRequestTimeout(configurer.getTimeout());
	}
	adapter.setCallableInterceptors(configurer.getCallableInterceptors());
	adapter.setDeferredResultInterceptors(configurer.getDeferredResultInterceptors());

	return adapter;
}
 
Example 10
@SuppressWarnings("unchecked")
private void verifyResponseBodyAdvice(Object bean) {
	assertNotNull(bean);
	Object value = new DirectFieldAccessor(bean).getPropertyValue("responseBodyAdvice");
	assertNotNull(value);
	assertTrue(value instanceof List);
	List<ResponseBodyAdvice> converters = (List<ResponseBodyAdvice>) value;
	assertTrue(converters.get(0) instanceof JsonViewResponseBodyAdvice);
}
 
Example 11
@SuppressWarnings("unchecked")
private void verifyRequestResponseBodyAdvice(Object bean) {
	assertNotNull(bean);
	Object value = new DirectFieldAccessor(bean).getPropertyValue("requestResponseBodyAdvice");
	assertNotNull(value);
	assertTrue(value instanceof List);
	List<ResponseBodyAdvice> converters = (List<ResponseBodyAdvice>) value;
	assertTrue(converters.get(0) instanceof JsonViewRequestBodyAdvice);
	assertTrue(converters.get(1) instanceof JsonViewResponseBodyAdvice);
}