Search in sources :

Example 6 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project starcoin-search by starcoinorg.

the class FastjsonConfiguration method fastjsonConverter.

@Bean
public HttpMessageConverters fastjsonConverter() {
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero);
    FastJsonHttpMessageConverter fastjson = new FastJsonHttpMessageConverter();
    fastjson.setFastJsonConfig(fastJsonConfig);
    return new HttpMessageConverters(fastjson);
}
Also used : FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) HttpMessageConverters(org.springframework.boot.autoconfigure.http.HttpMessageConverters) Bean(org.springframework.context.annotation.Bean)

Example 7 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project springboot-templet-start by thedestiny.

the class WebConfiguration method fastJsonHttpMessageConverter.

@Bean
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    converter.setFeatures(SerializerFeature.DisableCircularReferenceDetect);
    converter.setCharset(Charset.forName("UTF-8"));
    return converter;
}
Also used : FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) Bean(org.springframework.context.annotation.Bean)

Example 8 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project ice by zjn-zjn.

the class WebMvcConfig method configureMessageConverters.

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    /*make default jackson change to fastjson*/
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON);
    fastConverter.setSupportedMediaTypes(fastMediaTypes);
    fastConverter.setFastJsonConfig(fastJsonConfig);
    converters.add(0, fastConverter);
}
Also used : FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) ArrayList(java.util.ArrayList) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) MediaType(org.springframework.http.MediaType)

Example 9 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project eagle-oj-api by Eagle-OJ.

the class WebMvcConfigurer method configureMessageConverters.

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    converters.add(converter);
}
Also used : FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter)

Example 10 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project fastjson by alibaba.

the class FastJsonHttpMessageConverterTest method test_read.

@SuppressWarnings("deprecation")
public void test_read() throws Exception {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    converter.setCharset(Charset.forName("UTF-8"));
    Assert.assertEquals(Charset.forName("UTF-8"), converter.getCharset());
    converter.setFeatures(SerializerFeature.BrowserCompatible);
    Assert.assertEquals(1, converter.getFeatures().length);
    Assert.assertEquals(SerializerFeature.BrowserCompatible, converter.getFeatures()[0]);
    Assert.assertNull(converter.getDateFormat());
    converter.setDateFormat("yyyyMMdd");
    converter.setFilters(serializeFilter);
    Assert.assertEquals(1, converter.getFilters().length);
    Assert.assertEquals(serializeFilter, converter.getFilters()[0]);
    converter.addSerializeFilter(serializeFilter);
    Assert.assertEquals(2, converter.getFilters().length);
    converter.addSerializeFilter(null);
    converter.setSupportedMediaTypes(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON_UTF8 }));
    Assert.assertEquals(1, converter.getSupportedMediaTypes().size());
    Method method = FastJsonHttpMessageConverter.class.getDeclaredMethod("supports", Class.class);
    method.setAccessible(true);
    method.invoke(converter, int.class);
    HttpInputMessage input = new HttpInputMessage() {

        public HttpHeaders getHeaders() {
            // TODO Auto-generated method stub
            return null;
        }

        public InputStream getBody() throws IOException {
            return new ByteArrayInputStream("{\"id\":123}".getBytes(Charset.forName("UTF-8")));
        }
    };
    VO vo = (VO) converter.read(VO.class, input);
    Assert.assertEquals(123, vo.getId());
    final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    HttpOutputMessage out = new HttpOutputMessage() {

        public HttpHeaders getHeaders() {
            return new HttpHeaders();
        }

        public OutputStream getBody() throws IOException {
            return byteOut;
        }
    };
    converter.write(vo, MediaType.TEXT_PLAIN, out);
    byte[] bytes = byteOut.toByteArray();
    Assert.assertEquals("{\"id\":\"123\"}", new String(bytes, "UTF-8"));
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) HttpHeaders(org.springframework.http.HttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpOutputMessage(org.springframework.http.HttpOutputMessage) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) MediaType(org.springframework.http.MediaType) Method(java.lang.reflect.Method) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

FastJsonHttpMessageConverter (com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter)42 FastJsonConfig (com.alibaba.fastjson.support.config.FastJsonConfig)33 Bean (org.springframework.context.annotation.Bean)22 MediaType (org.springframework.http.MediaType)17 ArrayList (java.util.ArrayList)16 HttpMessageConverters (org.springframework.boot.autoconfigure.http.HttpMessageConverters)7 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)5 HttpHeaders (org.springframework.http.HttpHeaders)5 HttpOutputMessage (org.springframework.http.HttpOutputMessage)5 RestTemplate (org.springframework.web.client.RestTemplate)4 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)3 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HttpMessageConverters (org.springframework.boot.autoconfigure.web.HttpMessageConverters)2 HttpInputMessage (org.springframework.http.HttpInputMessage)2 GsonHttpMessageConverter (org.springframework.http.converter.json.GsonHttpMessageConverter)2 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)2 SimpleDateFormatSerializer (com.alibaba.fastjson.serializer.SimpleDateFormatSerializer)1