Java Code Examples for org.springframework.boot.autoconfigure.http.HttpMessageConverters

The following examples show how to use org.springframework.boot.autoconfigure.http.HttpMessageConverters. 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
Source Project: open-cloud   Source File: ApiConfiguration.java    License: MIT License 6 votes vote down vote up
/**
 * 转换器全局配置
 *
 * @param converters
 * @return
 */
@Bean
public HttpMessageConverters httpMessageConverters(List<HttpMessageConverter<?>> converters) {
    MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
    ObjectMapper objectMapper = new ObjectMapper();
    // 忽略为空的字段
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    objectMapper.getSerializationConfig().withFeatures(
            SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    /**
     * 序列换成json时,将所有的long变成string
     * js中long过长精度丢失
     */
    SimpleModule simpleModule = new SimpleModule();
    simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
    simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
    objectMapper.registerModule(simpleModule);
    jackson2HttpMessageConverter.setObjectMapper(objectMapper);
    log.info("MappingJackson2HttpMessageConverter [{}]", jackson2HttpMessageConverter);
    return new HttpMessageConverters(jackson2HttpMessageConverter);
}
 
Example 2
@Bean
    public HttpMessageConverters fastJsonConfigure(){
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //日期格式化
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        //修改配置返回内容的过滤
        fastJsonConfig.setSerializerFeatures(
//                SerializerFeature. WriteMapNullValue,//是否输出值为null的字段,默认为false
                SerializerFeature.WriteDateUseDateFormat//格式化标签
//        		SerializerFeature.WriteNullListAsEmpty  ,//List字段如果为null,输出为[],而非null
//                SerializerFeature.DisableCircularReferenceDetect,//消除对同一对象循环引用的问题,默认为false(如果不配置有可能会进入死循环)
//                SerializerFeature.WriteMapNullValue,//是否输出值为null的字段,默认为false。
//                SerializerFeature.WriteNullStringAsEmpty//字符类型字段如果为null,输出为"",而非null
//                SerializerFeature.WriteNullBooleanAsFalse//Boolean字段如果为null,输出为false,而非null
        );
        converter.setFastJsonConfig(fastJsonConfig);
        //处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        converter.setSupportedMediaTypes(fastMediaTypes);
        converter.setFastJsonConfig(fastJsonConfig);
        //将fastjson添加到视图消息转换器列表内
        return new HttpMessageConverters(converter);
    }
 
Example 3
@Bean
public HttpMessageConverters fastJsonConfigure(){
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    //日期格式化
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    //修改配置返回内容的过滤
    fastJsonConfig.setSerializerFeatures(
            SerializerFeature. WriteMapNullValue,//是否输出值为null的字段,默认为false
            SerializerFeature.WriteDateUseDateFormat,//格式化标签
    		SerializerFeature.WriteNullListAsEmpty , //List字段如果为null,输出为[],而非null
            SerializerFeature.DisableCircularReferenceDetect,//消除对同一对象循环引用的问题,默认为false(如果不配置有可能会进入死循环)
            SerializerFeature.WriteMapNullValue,//是否输出值为null的字段,默认为false。
            SerializerFeature.WriteNullStringAsEmpty,//字符类型字段如果为null,输出为"",而非null
            SerializerFeature.WriteNullBooleanAsFalse//Boolean字段如果为null,输出为false,而非null
    );
    converter.setFastJsonConfig(fastJsonConfig);
    //处理中文乱码问题
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    converter.setSupportedMediaTypes(fastMediaTypes);
    converter.setFastJsonConfig(fastJsonConfig);
    //将fastjson添加到视图消息转换器列表内
    return new HttpMessageConverters(converter);
}
 
Example 4
Source Project: sophia_scaffolding   Source File: RedisConfig.java    License: Apache License 2.0 6 votes vote down vote up
/**
 *  使用@Bean注入fastJsonHttpMessageConvert,fastJson序列化
 */
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
    // 1、需要先定义一个 convert 转换消息的对象;
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

    //2-1 处理中文乱码问题
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    fastConverter.setSupportedMediaTypes(fastMediaTypes);

    //3、在convert中添加配置信息.
    fastConverter.setFastJsonConfig(fastJsonConfig);
    HttpMessageConverter<?> converter = fastConverter;
    return new HttpMessageConverters(converter);
}
 
Example 5
Source Project: sophia_scaffolding   Source File: RedisConfig.java    License: Apache License 2.0 6 votes vote down vote up
/**
 *  使用@Bean注入fastJsonHttpMessageConvert,fastJson序列化
 */
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
    // 1、需要先定义一个 convert 转换消息的对象;
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

    //2-1 处理中文乱码问题
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    fastConverter.setSupportedMediaTypes(fastMediaTypes);

    //3、在convert中添加配置信息.
    fastConverter.setFastJsonConfig(fastJsonConfig);
    HttpMessageConverter<?> converter = fastConverter;
    return new HttpMessageConverters(converter);
}
 
Example 6
Source Project: sophia_scaffolding   Source File: RedisConfig.java    License: Apache License 2.0 6 votes vote down vote up
/**
 *  使用@Bean注入fastJsonHttpMessageConvert,fastJson序列化
 */
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
    // 1、需要先定义一个 convert 转换消息的对象;
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

    //2-1 处理中文乱码问题
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    fastConverter.setSupportedMediaTypes(fastMediaTypes);

    //3、在convert中添加配置信息.
    fastConverter.setFastJsonConfig(fastJsonConfig);
    HttpMessageConverter<?> converter = fastConverter;
    return new HttpMessageConverters(converter);
}
 
Example 7
Source Project: xmfcn-spring-cloud   Source File: BeanConfig.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 替换springMVC JSON格式化工具为FastJSON,同时支持输出value=null的字段
 *
 * @return http message converters
 */
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
    //1.需要定义一个convert转换消息的对象;
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    //2:添加fastJson的配置信息;
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
    fastJsonConfig.setSerializeFilters();
    //3处理中文乱码问题
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    //4.在convert中添加配置信息.
    fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
    fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
    return new HttpMessageConverters(fastJsonHttpMessageConverter);

}
 
Example 8
Source Project: xmfcn-spring-cloud   Source File: BeanConfig.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 替换springMVC JSON格式化工具为FastJSON,同时支持输出value=null的字段
 *
 * @return http message converters
 */
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
    //1.需要定义一个convert转换消息的对象;
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    //2:添加fastJson的配置信息;
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
    fastJsonConfig.setSerializeFilters();
    //3处理中文乱码问题
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    //4.在convert中添加配置信息.
    fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
    fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
    return new HttpMessageConverters(fastJsonHttpMessageConverter);

}
 
Example 9
Source Project: xmfcn-spring-cloud   Source File: HttpBasicAuthConfig.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 替换springMVC JSON格式化工具为FastJSON,同时支持输出value=null的字段
 *
 * @return http message converters
 */
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
    //1.需要定义一个convert转换消息的对象;
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    //2:添加fastJson的配置信息;
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
    fastJsonConfig.setSerializeFilters();
    //3处理中文乱码问题
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    //4.在convert中添加配置信息.
    fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
    fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
    return new HttpMessageConverters((HttpMessageConverter<?>) fastJsonHttpMessageConverter);

}
 
Example 10
Source Project: seed   Source File: FastjsonConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public HttpMessageConverters fastjsonConverter(){
    List<SerializerFeature> serializerFeatureList = new ArrayList<>();
    serializerFeatureList.add(SerializerFeature.PrettyFormat);
    serializerFeatureList.add(SerializerFeature.QuoteFieldNames);
    serializerFeatureList.add(SerializerFeature.WriteMapNullValue);
    serializerFeatureList.add(SerializerFeature.WriteNullListAsEmpty);
    serializerFeatureList.add(SerializerFeature.WriteNullNumberAsZero);
    serializerFeatureList.add(SerializerFeature.WriteNullStringAsEmpty);
    serializerFeatureList.add(SerializerFeature.WriteNullBooleanAsFalse);
    serializerFeatureList.add(SerializerFeature.WriteDateUseDateFormat);
    SerializerFeature[] serializerFeatures = new SerializerFeature[serializerFeatureList.size()];
    serializerFeatureList.toArray(serializerFeatures);
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setCharset(StandardCharsets.UTF_8);
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    fastJsonConfig.setSerializerFeatures(serializerFeatures);
    FastJsonHttpMessageConverter fastjson = new FastJsonHttpMessageConverter();
    fastjson.setFastJsonConfig(fastJsonConfig);
    return new HttpMessageConverters(fastjson);
}
 
Example 11
Source Project: seed   Source File: FastjsonConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public HttpMessageConverters fastjsonConverter(){
    List<SerializerFeature> serializerFeatureList = new ArrayList<>();
    serializerFeatureList.add(SerializerFeature.PrettyFormat);
    serializerFeatureList.add(SerializerFeature.QuoteFieldNames);
    serializerFeatureList.add(SerializerFeature.WriteMapNullValue);
    serializerFeatureList.add(SerializerFeature.WriteNullListAsEmpty);
    serializerFeatureList.add(SerializerFeature.WriteNullNumberAsZero);
    serializerFeatureList.add(SerializerFeature.WriteNullStringAsEmpty);
    serializerFeatureList.add(SerializerFeature.WriteNullBooleanAsFalse);
    serializerFeatureList.add(SerializerFeature.WriteDateUseDateFormat);
    SerializerFeature[] serializerFeatures = new SerializerFeature[serializerFeatureList.size()];
    serializerFeatureList.toArray(serializerFeatures);
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setCharset(StandardCharsets.UTF_8);
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    fastJsonConfig.setSerializerFeatures(serializerFeatures);
    FastJsonHttpMessageConverter fastjson = new FastJsonHttpMessageConverter();
    fastjson.setFastJsonConfig(fastJsonConfig);
    return new HttpMessageConverters(fastjson);
}
 
Example 12
Source Project: seed   Source File: FastjsonConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public HttpMessageConverters fastjsonConverter(){
    List<SerializerFeature> serializerFeatureList = new ArrayList<>();
    serializerFeatureList.add(SerializerFeature.PrettyFormat);
    serializerFeatureList.add(SerializerFeature.QuoteFieldNames);
    serializerFeatureList.add(SerializerFeature.WriteMapNullValue);
    serializerFeatureList.add(SerializerFeature.WriteNullListAsEmpty);
    serializerFeatureList.add(SerializerFeature.WriteNullNumberAsZero);
    serializerFeatureList.add(SerializerFeature.WriteNullStringAsEmpty);
    serializerFeatureList.add(SerializerFeature.WriteNullBooleanAsFalse);
    serializerFeatureList.add(SerializerFeature.WriteDateUseDateFormat);
    SerializerFeature[] serializerFeatures = new SerializerFeature[serializerFeatureList.size()];
    serializerFeatureList.toArray(serializerFeatures);
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setCharset(StandardCharsets.UTF_8);
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    fastJsonConfig.setSerializerFeatures(serializerFeatures);
    FastJsonHttpMessageConverter fastjson = new FastJsonHttpMessageConverter();
    fastjson.setFastJsonConfig(fastJsonConfig);
    return new HttpMessageConverters(fastjson);
}
 
Example 13
@Bean
@Lazy
@ConditionalOnMissingBean
public RestTemplateBuilder restTemplateBuilder(ObjectProvider<HttpMessageConverters> messageConverters,
                                               ObjectProvider<RestTemplateCustomizer> restTemplateCustomizers,
                                               ObjectProvider<RestTemplateRequestCustomizer<?>> restTemplateRequestCustomizers,
                                               OkHttpClient okHttpClient) {
    RestTemplateBuilder builder = new RestTemplateBuilder();
    HttpMessageConverters converters = messageConverters.getIfUnique();
    if (converters != null) {
        builder = builder.messageConverters(converters.getConverters());
    }
    builder = addCustomizers(builder, restTemplateCustomizers, RestTemplateBuilder::customizers);
    builder = addCustomizers(builder, restTemplateRequestCustomizers, RestTemplateBuilder::requestCustomizers);

    builder = builder.requestFactory(() -> new OkHttp3ClientHttpRequestFactory(okHttpClient));

    return builder;
}
 
Example 14
Source Project: feign-form   Source File: DownloadClient.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public Decoder feignDecoder () {
  val springConverters = messageConverters.getObject().getConverters();
  val decoderConverters = new ArrayList<HttpMessageConverter<?>>(springConverters.size() + 1);

  decoderConverters.addAll(springConverters);
  decoderConverters.add(new SpringManyMultipartFilesReader(4096));

  val httpMessageConverters = new HttpMessageConverters(decoderConverters);

  return new SpringDecoder(new ObjectFactory<HttpMessageConverters>() {

    @Override
    public HttpMessageConverters getObject () {
        return httpMessageConverters;
    }
  });
}
 
Example 15
@Bean
@Lazy
@ConditionalOnMissingBean
public RestTemplateBuilder restTemplateBuilder(ObjectProvider<HttpMessageConverters> messageConverters,
                                               ObjectProvider<RestTemplateCustomizer> restTemplateCustomizers,
                                               ObjectProvider<RestTemplateRequestCustomizer<?>> restTemplateRequestCustomizers,
                                               OkHttpClient okHttpClient) {
    RestTemplateBuilder builder = new RestTemplateBuilder();
    HttpMessageConverters converters = messageConverters.getIfUnique();
    if (converters != null) {
        builder = builder.messageConverters(converters.getConverters());
    }
    builder = addCustomizers(builder, restTemplateCustomizers, RestTemplateBuilder::customizers);
    builder = addCustomizers(builder, restTemplateRequestCustomizers, RestTemplateBuilder::requestCustomizers);

    builder = builder.requestFactory(() -> new OkHttp3ClientHttpRequestFactory(okHttpClient));

    return builder;
}
 
Example 16
Source Project: XS2A-Sandbox   Source File: TppUiBeFeignConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public Encoder feignEncoder() {
    objectMapper.addMixIn(ScaUserDataTO.class, ScaUserDataMixedIn.class)
        .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter(objectMapper);
    ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(jacksonConverter);
    return new SpringEncoder(objectFactory);
}
 
Example 17
Source Project: XS2A-Sandbox   Source File: FeignConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public Decoder feignDecoder() {
	ObjectMapper objectMapper = new ObjectMapper();
	objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
	objectMapper.registerModule(new JavaTimeModule());
	HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter(objectMapper);
	ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(jacksonConverter);
	return new ResponseEntityDecoder(new SpringDecoder(objectFactory));
}
 
Example 18
Source Project: kbear   Source File: RestConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public HttpMessageConverters customConverters() {
    try {
        HttpMessageConverter<Message> converter = newCustomProtobufMessageConverter();
        return new HttpMessageConverters(converter);
    } catch (Exception e) {
        throw new IllegalStateException("cannot construct custom protobuf converter", e);
    }
}
 
Example 19
Source Project: exchange-gateway-rest   Source File: TestSupport.java    License: Apache License 2.0 5 votes vote down vote up
@Autowired
public void setConverters(HttpMessageConverters converters) {
    mappingJackson2HttpMessageConverter = converters.getConverters().stream()
            .filter(converter -> converter instanceof MappingJackson2HttpMessageConverter)
            .findFirst()
            .orElseThrow((() -> new IllegalStateException("the JSON message converter can not be null")));
}
 
Example 20
/**
 * http message converter fastjson实现实例
 * 通过fastjson方式进行格式化返回json字符串
 *
 * @return http message converter
 */
@Bean
@ConditionalOnMissingBean
HttpMessageConverters fastJsonHttpMessageConverters() {
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    //创建fastJson配置实体类
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(
            SerializerFeature.DisableCircularReferenceDetect,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullListAsEmpty,
            SerializerFeature.WriteNullStringAsEmpty,
            SerializerFeature.WriteNullNumberAsZero,
            SerializerFeature.WriteDateUseDateFormat,
            SerializerFeature.WriteNullBooleanAsFalse
    );

    // get customer define value filters
    List<String> packages = AutoConfigurationPackages.get(beanFactory);
    // get plugin define value filters
    packages.addAll(Arrays.asList(VALUE_FILTER_PACKAGE));

    fastJsonConfig.setSerializeFilters(getDefineFilters(packages));

    fastConverter.setFastJsonConfig(fastJsonConfig);
    return new HttpMessageConverters(fastConverter);
}
 
Example 21
@Test
public void testEncodeWhenProtobufNotInClasspath() {
	ObjectFactory<HttpMessageConverters> converters = new ObjectFactory<HttpMessageConverters>() {
		@Override
		public HttpMessageConverters getObject() throws BeansException {
			return new HttpMessageConverters(new StringHttpMessageConverter());
		}
	};
	RequestTemplate requestTemplate = new RequestTemplate();
	requestTemplate.method(POST);
	new SpringEncoder(converters).encode("a=b", String.class, requestTemplate);
}
 
Example 22
private SpringEncoder newEncoder() {
	ObjectFactory<HttpMessageConverters> converters = new ObjectFactory<HttpMessageConverters>() {
		@Override
		public HttpMessageConverters getObject() throws BeansException {
			return new HttpMessageConverters(new ProtobufHttpMessageConverter());
		}
	};
	return new SpringEncoder(converters);
}
 
Example 23
Source Project: zuihou-admin-cloud   Source File: CorsConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 升级版本后, 不加这个 gateway 使用feign会报错,不知道什么原因
 *
 * @return
 */
@Bean
public Decoder feignFormDecoder() {
    List<HttpMessageConverter<?>> converters = new RestTemplate().getMessageConverters();
    ObjectFactory<HttpMessageConverters> factory = () -> new HttpMessageConverters(converters);
    return new ResponseEntityDecoder(new SpringDecoder(factory));
}
 
Example 24
Source Project: spring-cloud-shop   Source File: ApplicationConfiguration.java    License: MIT License 5 votes vote down vote up
@Bean
public HttpMessageConverters fastJsonConfigure() {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    converter.setFastJsonConfig(fastJsonConfig);

    List<MediaType> supportedMediaTypes = new ArrayList<>();

    supportedMediaTypes.add(MediaType.APPLICATION_JSON);
    supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
    supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
    supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
    supportedMediaTypes.add(MediaType.APPLICATION_PDF);
    supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
    supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
    supportedMediaTypes.add(MediaType.APPLICATION_XML);
    supportedMediaTypes.add(MediaType.IMAGE_GIF);
    supportedMediaTypes.add(MediaType.IMAGE_JPEG);
    supportedMediaTypes.add(MediaType.IMAGE_PNG);
    supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
    supportedMediaTypes.add(MediaType.TEXT_HTML);
    supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
    supportedMediaTypes.add(MediaType.TEXT_PLAIN);
    supportedMediaTypes.add(MediaType.TEXT_XML);
    converter.setSupportedMediaTypes(supportedMediaTypes);
    return new HttpMessageConverters(converter);
}
 
Example 25
Source Project: logistics-back   Source File: LogisticsApplication.java    License: MIT License 5 votes vote down vote up
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
	// 1.需要定义一个Convert转换消息的对象
	FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
	// 2.添加fastjson的配置信息,比如是否要格式化返回的json数据
	FastJsonConfig fastJsonConfig = new FastJsonConfig();
	fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
	// 3.在convert中添加配置信息
	fastConverter.setFastJsonConfig(fastJsonConfig);

	HttpMessageConverter<?> converter = fastConverter;
	return new HttpMessageConverters(converter);
}
 
Example 26
Source Project: mercury   Source File: HttpConverterLoader.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public HttpMessageConverters getMessageConverters() {
    log.info("Loading HTTP serializers");
    HttpConverterJson json = new HttpConverterJson();
    HttpConverterXml xml = new HttpConverterXml();
    HttpConverterHtml html = new HttpConverterHtml();
    HttpConverterText text = new HttpConverterText();
    return new HttpMessageConverters(json, xml, html, text);
}
 
Example 27
@PostConstruct
public void init() {
	HttpMessageConverters httpMessageConverters = this.httpMessageConverters
			.getIfAvailable();

	httpMessageConverterResolver = new HttpMessageConverterResolver(
			httpMessageConverters == null ? Collections.emptyList()
					: httpMessageConverters.getConverters(),
			getClassLoader());
}
 
Example 28
Source Project: bookmark   Source File: Application.java    License: MIT License 5 votes vote down vote up
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
    // 1.定义一个converters转换消息的对象
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    // 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
    // 3.在converter中添加配置信息
    fastConverter.setFastJsonConfig(fastJsonConfig);
    // 5.返回HttpMessageConverters对象
    return new HttpMessageConverters(fastConverter);
}
 
Example 29
Source Project: apollo   Source File: AdminServiceAddressLocator.java    License: Apache License 2.0 5 votes vote down vote up
public AdminServiceAddressLocator(
    final HttpMessageConverters httpMessageConverters,
    final PortalSettings portalSettings,
    final RestTemplateFactory restTemplateFactory,
    final PortalMetaDomainService portalMetaDomainService
) {
  this.portalSettings = portalSettings;
  this.restTemplateFactory = restTemplateFactory;
  this.portalMetaDomainService = portalMetaDomainService;
}
 
Example 30
Source Project: apollo   Source File: HttpMessageConverterConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public HttpMessageConverters messageConverters() {
  GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
  gsonHttpMessageConverter.setGson(
          new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create());
  final List<HttpMessageConverter<?>> converters = Lists.newArrayList(
          new ByteArrayHttpMessageConverter(), new StringHttpMessageConverter(),
          new AllEncompassingFormHttpMessageConverter(), gsonHttpMessageConverter);
  return new HttpMessageConverters() {
    @Override
    public List<HttpMessageConverter<?>> getConverters() {
      return converters;
    }
  };
}