use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project spring-boot by Linda-Tan.
the class RestTemplateConfig method serverRest.
@Bean("serverRest")
public RestTemplate serverRest() {
// http://www.jianshu.com/p/c9644755dd5e
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new FastJsonHttpMessageConverter());
return restTemplate;
}
use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project neweagle-api by apgzs.
the class CustomMVCConfiguration method configureMessageConverters.
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(responseBodyConverter());
// 使用fastjson
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project Hidoc by yuzhengyang.
the class FastJsonConverterConfig method fastJsonHttpMessageConverters.
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullBooleanAsFalse);
fastConverter.setFastJsonConfig(fastJsonConfig);
// 全局指定了日期格式
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
// 该设置目的,为了兼容jackson
fastConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_UTF8, MediaType.APPLICATION_OCTET_STREAM));
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project fastjson by alibaba.
the class DateFormatPriorityTest method test_for_fastJsonConfig.
public void test_for_fastJsonConfig() throws IOException {
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy-MM.dd");
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
converter.setFastJsonConfig(config);
converter.canRead(VO.class, MediaType.APPLICATION_JSON_UTF8);
converter.canWrite(VO.class, MediaType.APPLICATION_JSON_UTF8);
final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
HttpOutputMessage out = new HttpOutputMessage() {
public HttpHeaders getHeaders() {
return new HttpHeaders() {
private static final long serialVersionUID = 1L;
@Override
public MediaType getContentType() {
return MediaType.APPLICATION_JSON;
}
};
}
public OutputStream getBody() throws IOException {
return byteOut;
}
};
VO vo = new VO();
vo.setDate(calendar.getTime());
converter.write(vo, VO.class, MediaType.APPLICATION_JSON_UTF8, out);
byte[] bytes = byteOut.toByteArray();
String jsonString = new String(bytes, "UTF-8");
Assert.assertEquals("{\"date\":\"1995-10.26\"}", jsonString);
}
use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project fastjson by alibaba.
the class FastJsonHttpMessageConverterTest method test_1.
public void test_1() throws Exception {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
Assert.assertNotNull(converter.getFastJsonConfig());
converter.setFastJsonConfig(new FastJsonConfig());
converter.canRead(VO.class, MediaType.APPLICATION_JSON_UTF8);
converter.canWrite(VO.class, MediaType.APPLICATION_JSON_UTF8);
converter.canRead(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
converter.canWrite(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
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, 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, VO.class, MediaType.TEXT_PLAIN, out);
byte[] bytes = byteOut.toByteArray();
Assert.assertEquals("{\"id\":123}", new String(bytes, "UTF-8"));
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
converter.write(vo, VO.class, null, out);
converter.write(vo, VO.class, MediaType.ALL, out);
HttpOutputMessage out2 = new HttpOutputMessage() {
public HttpHeaders getHeaders() {
return new HttpHeaders() {
private static final long serialVersionUID = 1L;
@Override
public MediaType getContentType() {
return MediaType.APPLICATION_JSON;
}
@Override
public long getContentLength() {
return 1;
}
};
}
public OutputStream getBody() throws IOException {
return byteOut;
}
};
converter.write(vo, VO.class, MediaType.ALL, out2);
}
Aggregations