Search in sources :

Example 1 with JsonViewResponseBodyAdvice

use of org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice in project spring-framework by spring-projects.

the class WebMvcConfigurationSupport method addDefaultHandlerExceptionResolvers.

/**
	 * 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.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());
}
Also used : ExceptionHandlerExceptionResolver(org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver) ResponseStatusExceptionResolver(org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver) JsonViewResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver)

Example 2 with JsonViewResponseBodyAdvice

use of org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice in project spring-framework by spring-projects.

the class WebMvcConfigurationSupport method requestMappingHandlerAdapter.

/**
	 * 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.singletonList(new JsonViewRequestBodyAdvice()));
        adapter.setResponseBodyAdvice(Collections.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;
}
Also used : JsonViewRequestBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewRequestBodyAdvice) JsonViewResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) Bean(org.springframework.context.annotation.Bean)

Example 3 with JsonViewResponseBodyAdvice

use of org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice in project spring-framework by spring-projects.

the class TestPathHelper method verifyResponseBodyAdvice.

@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);
}
Also used : JsonViewResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice) ResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) JsonViewResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice) List(java.util.List)

Example 4 with JsonViewResponseBodyAdvice

use of org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice in project spring-framework by spring-projects.

the class TestPathHelper method verifyRequestResponseBodyAdvice.

@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);
}
Also used : JsonViewResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice) ResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice) JsonViewRequestBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewRequestBodyAdvice) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) JsonViewResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice) List(java.util.List)

Aggregations

JsonViewResponseBodyAdvice (org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice)4 List (java.util.List)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 JsonViewRequestBodyAdvice (org.springframework.web.servlet.mvc.method.annotation.JsonViewRequestBodyAdvice)2 ResponseBodyAdvice (org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice)2 Bean (org.springframework.context.annotation.Bean)1 ResponseStatusExceptionResolver (org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver)1 ExceptionHandlerExceptionResolver (org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver)1 RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter)1 DefaultHandlerExceptionResolver (org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver)1