Search in sources :

Example 91 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project spring-cloud-open-service-broker by spring-cloud.

the class CatalogControllerIntegrationTest method setUp.

@Before
public void setUp() {
    this.mockMvc = MockMvcBuilders.standaloneSetup(controller).setMessageConverters(new MappingJackson2HttpMessageConverter()).build();
    serviceDefinition = ServiceFixture.getSimpleService();
    Catalog catalog = Catalog.builder().serviceDefinitions(serviceDefinition).build();
    when(catalogService.getCatalog()).thenReturn(catalog);
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) Catalog(org.springframework.cloud.servicebroker.model.catalog.Catalog) Before(org.junit.Before)

Example 92 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project entando-core by entando.

the class AbstractControllerTest method createExceptionResolver.

protected ExceptionHandlerExceptionResolver createExceptionResolver() {
    final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("rest/messages");
    messageSource.setUseCodeAsDefaultMessage(true);
    ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver() {

        @Override
        protected ServletInvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handlerMethod, Exception exception) {
            Method method = new ExceptionHandlerMethodResolver(RestExceptionHandler.class).resolveMethod(exception);
            RestExceptionHandler validationHandler = new RestExceptionHandler();
            validationHandler.setMessageSource(messageSource);
            return new ServletInvocableHandlerMethod(validationHandler, method);
        }
    };
    exceptionResolver.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    exceptionResolver.afterPropertiesSet();
    return exceptionResolver;
}
Also used : ExceptionHandlerExceptionResolver(org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) RestExceptionHandler(org.entando.entando.web.common.handlers.RestExceptionHandler) ServletInvocableHandlerMethod(org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod) ExceptionHandlerMethodResolver(org.springframework.web.method.annotation.ExceptionHandlerMethodResolver) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) ServletInvocableHandlerMethod(org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) ServletInvocableHandlerMethod(org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException)

Example 93 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project sailfish-mfa by picos-io.

the class MfaWebMvcConfigurer method addReturnValueHandlers.

@Override
public void addReturnValueHandlers(final List<HandlerMethodReturnValueHandler> returnValueHandlers) {
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
    messageConverters.add(new MappingJackson2HttpMessageConverter());
    returnValueHandlers.add(new RequestResponseBodyMethodProcessor(messageConverters));
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ArrayList(java.util.ArrayList) RequestResponseBodyMethodProcessor(org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor)

Example 94 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project open-kilda by telstra.

the class WebConfig method restTemplate.

/**
 * Rest template for performing REST calls.
 */
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
    RestTemplate restTemplate = builder.build();
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
    restTemplate.getMessageConverters().add(converter);
    return restTemplate;
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) RestTemplate(org.springframework.web.client.RestTemplate) Bean(org.springframework.context.annotation.Bean)

Example 95 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project topcom-cloud by 545314690.

the class MyWebAppConfigurer method extendMessageConverters.

/**
 * 添加返回结果缩进支持,如果存在pretty参数,则返回结果添加缩进
 * @param converters
 */
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.replaceAll(c -> {
        if (c instanceof MappingJackson2HttpMessageConverter) {
            MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(mapper) {

                @Override
                protected void writePrefix(JsonGenerator generator, Object object) throws IOException {
                    RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
                    if (attributes != null && attributes instanceof ServletRequestAttributes) {
                        String attribute = ((ServletRequestAttributes) attributes).getRequest().getParameter("pretty");
                        if (attribute != null) {
                            generator.setPrettyPrinter(new DefaultPrettyPrinter());
                        }
                    }
                    super.writePrefix(generator, object);
                }
            };
            return converter;
        } else {
            return c;
        }
    });
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Aggregations

MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)122 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)51 ArrayList (java.util.ArrayList)46 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)38 Test (org.junit.jupiter.api.Test)28 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)25 RestTemplate (org.springframework.web.client.RestTemplate)25 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)21 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)20 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)19 HandlerMethod (org.springframework.web.method.HandlerMethod)17 Method (java.lang.reflect.Method)16 Test (org.junit.Test)16 MethodParameter (org.springframework.core.MethodParameter)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 Bean (org.springframework.context.annotation.Bean)14 List (java.util.List)9 HttpEntity (org.springframework.http.HttpEntity)7 MediaType (org.springframework.http.MediaType)6 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)6