Search in sources :

Example 66 with StringHttpMessageConverter

use of org.springframework.http.converter.StringHttpMessageConverter in project paascloud-master by paascloud.

the class QQOAuth2Template method createRestTemplate.

/**
 * Create rest template rest template.
 *
 * @return the rest template
 */
@Override
protected RestTemplate createRestTemplate() {
    RestTemplate restTemplate = super.createRestTemplate();
    restTemplate.getMessageConverters().add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
    return restTemplate;
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter)

Example 67 with StringHttpMessageConverter

use of org.springframework.http.converter.StringHttpMessageConverter in project snow-owl by b2ihealthcare.

the class SnowOwlApiConfig method configureMessageConverters.

@Override
public void configureMessageConverters(final List<HttpMessageConverter<?>> converters) {
    final StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(Charsets.UTF_8);
    stringConverter.setWriteAcceptCharset(false);
    converters.add(stringConverter);
    converters.add(new ByteArrayHttpMessageConverter());
    converters.add(new ResourceHttpMessageConverter());
    converters.add(new CsvMessageConverter());
    // XXX using null value here as Spring calls a proxied method anyway which returns an already configured instance, see mapping2JacksonHttpMessageConverter Bean method
    converters.add(mapping2JacksonHttpMessageConverter(null));
}
Also used : ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) CsvMessageConverter(com.b2international.snowowl.core.rest.util.CsvMessageConverter)

Example 68 with StringHttpMessageConverter

use of org.springframework.http.converter.StringHttpMessageConverter in project littlefisher-system by littlefishercoder.

the class WebSpringMvcConfig method configureMessageConverters.

/**
 * Description: 消息转换器配置
 *
 * @param converters converters
 */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
    ObjectMapper om = jacksonConverter.getObjectMapper();
    om.setDateFormat(new SimpleDateFormat(DateUtil.DATETIME_FORMAT_1));
    om.setLocale(Locale.CHINA);
    om.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    List<MediaType> mediaTypeList = Lists.newArrayList();
    mediaTypeList.add(new MediaType(MediaType.APPLICATION_JSON, Charsets.UTF_8));
    mediaTypeList.add(new MediaType(MediaType.TEXT_HTML, Charsets.UTF_8));
    jacksonConverter.setSupportedMediaTypes(mediaTypeList);
    StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(Charsets.UTF_8);
    stringConverter.setWriteAcceptCharset(false);
    // 保持以下顺序
    // "application/json" "application/*+json"
    converters.add(jacksonConverter);
    // "application/xml" "text/xml" "application/*+xml"
    converters.add(new Jaxb2RootElementHttpMessageConverter());
    // "application/xml" "text/xml" "application/*+xml"
    converters.add(new SourceHttpMessageConverter<>());
    // "application/x-www-form-urlencoded" "multipart/form-data"
    converters.add(new AllEncompassingFormHttpMessageConverter());
    // "application/octet-stream" "*/*"
    converters.add(new ByteArrayHttpMessageConverter());
    // "text/plain" "*/*"
    converters.add(stringConverter);
    // "*/*"
    converters.add(new ResourceHttpMessageConverter());
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) MediaType(org.springframework.http.MediaType) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) SimpleDateFormat(java.text.SimpleDateFormat) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter)

Example 69 with StringHttpMessageConverter

use of org.springframework.http.converter.StringHttpMessageConverter in project pancm_project by xuwujing.

the class RestTemplateHelper method getDefaultRestTemplate.

private RestTemplate getDefaultRestTemplate(int connectTimeoutSeconds, int readTimeoutSeconds) {
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    requestFactory.setConnectTimeout(connectTimeoutSeconds * 1000);
    requestFactory.setReadTimeout(readTimeoutSeconds * 1000);
    // HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
    // httpRequestFactory.setConnectionRequestTimeout(connectTimeoutSeconds * 1000);
    // httpRequestFactory.setConnectTimeout(readTimeoutSeconds * 1000);
    // httpRequestFactory.setReadTimeout(readTimeoutSeconds * 1000);
    RestTemplate restTemplate = new RestTemplate(requestFactory);
    // 换上fastjson
    List<HttpMessageConverter<?>> httpMessageConverterList = restTemplate.getMessageConverters();
    Iterator<HttpMessageConverter<?>> iterator = httpMessageConverterList.iterator();
    if (iterator.hasNext()) {
        HttpMessageConverter<?> converter = iterator.next();
        // 原有的String是ISO-8859-1编码 去掉
        if (converter instanceof StringHttpMessageConverter) {
            iterator.remove();
        }
        // 由于系统中默认有jackson 在转换json时自动会启用  但是我们不想使用它 可以直接移除或者将fastjson放在首位
        if (converter instanceof GsonHttpMessageConverter || converter instanceof MappingJackson2HttpMessageConverter) {
            iterator.remove();
        }
    }
    httpMessageConverterList.add(new StringHttpMessageConverter(Charset.forName("utf-8")));
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON);
    fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
    fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
    httpMessageConverterList.add(0, fastJsonHttpMessageConverter);
    return restTemplate;
}
Also used : SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) ArrayList(java.util.ArrayList) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) RestTemplate(org.springframework.web.client.RestTemplate) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter)

Example 70 with StringHttpMessageConverter

use of org.springframework.http.converter.StringHttpMessageConverter in project pancm_project by xuwujing.

the class RestTemplateHelper method getHttpsDefaultRestTemplate.

private RestTemplate getHttpsDefaultRestTemplate(int connectTimeoutSeconds, int readTimeoutSeconds) {
    HttpsClientRequestFactory requestFactory = new HttpsClientRequestFactory();
    requestFactory.setConnectTimeout(connectTimeoutSeconds * 1000);
    requestFactory.setReadTimeout(readTimeoutSeconds * 1000);
    // HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
    // httpRequestFactory.setConnectionRequestTimeout(connectTimeoutSeconds * 1000);
    // httpRequestFactory.setConnectTimeout(readTimeoutSeconds * 1000);
    // httpRequestFactory.setReadTimeout(readTimeoutSeconds * 1000);
    RestTemplate restTemplate = new RestTemplate(requestFactory);
    // 换上fastjson
    List<HttpMessageConverter<?>> httpMessageConverterList = restTemplate.getMessageConverters();
    Iterator<HttpMessageConverter<?>> iterator = httpMessageConverterList.iterator();
    if (iterator.hasNext()) {
        HttpMessageConverter<?> converter = iterator.next();
        // 原有的String是ISO-8859-1编码 去掉
        if (converter instanceof StringHttpMessageConverter) {
            iterator.remove();
        }
        // 由于系统中默认有jackson 在转换json时自动会启用  但是我们不想使用它 可以直接移除或者将fastjson放在首位
        if (converter instanceof GsonHttpMessageConverter || converter instanceof MappingJackson2HttpMessageConverter) {
            iterator.remove();
        }
    }
    httpMessageConverterList.add(new StringHttpMessageConverter(Charset.forName("utf-8")));
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON);
    fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
    fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
    httpMessageConverterList.add(0, fastJsonHttpMessageConverter);
    return restTemplate;
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) ArrayList(java.util.ArrayList) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) RestTemplate(org.springframework.web.client.RestTemplate) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter)

Aggregations

StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)93 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)48 ArrayList (java.util.ArrayList)38 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)27 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)24 RestTemplate (org.springframework.web.client.RestTemplate)23 Test (org.junit.jupiter.api.Test)21 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)18 Test (org.junit.Test)16 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)16 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)11 MediaType (org.springframework.http.MediaType)9 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)9 SerDeHttpMessageConverter (com.kixeye.chassis.transport.http.SerDeHttpMessageConverter)7 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)7 URI (java.net.URI)7 HashMap (java.util.HashMap)7 MethodParameter (org.springframework.core.MethodParameter)7 MapPropertySource (org.springframework.core.env.MapPropertySource)7 StandardEnvironment (org.springframework.core.env.StandardEnvironment)7