use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project fastjson by alibaba.
the class Issue3805 method test.
@Test
public void test() throws Exception {
String dateFormat = "yyyy";
TestModel model = new TestModel();
model.setCreateTime(LocalDateTime.of(2021, 5, 6, 7, 8, 9, 5));
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat(dateFormat);
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
converter.setFastJsonConfig(config);
converter.canRead(TestModel.class, MediaType.APPLICATION_JSON_UTF8);
converter.canWrite(TestModel.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;
}
};
converter.write(model, TestModel.class, MediaType.APPLICATION_JSON_UTF8, out);
byte[] bytes = byteOut.toByteArray();
String jsonString = new String(bytes, "UTF-8");
// System.out.println(jsonString);
Assert.assertEquals(jsonString, "{\"createTime\":\"2021\"}");
}
use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project fastjson by alibaba.
the class DateFormatPriorityTest method test_priority_01.
// Annotation + FastJsonConfig (Annotation优先)
public void test_priority_01() throws Exception {
// FastJsonConfig
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;
}
};
VO2 vo = new VO2();
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");
assertEquals("{\"date\":\"1995.10-26\"}", jsonString);
}
use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project Minecraft-Server-WebStore by Ba1oretto.
the class DefaultFastjsonConfig method fastJsonHttpMessageConverter.
@Bean
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
converter.setFastJsonConfig(fastjsonConfig());
return converter;
}
use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project springboot_op by SnailFastGo.
the class FastJsonConfiguration method configureMessageConverters.
/**
* 修改自定义消息转换器
* @param converters 消息转换器列表
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// 调用父类的配置
super.configureMessageConverters(converters);
// 创建fastJson消息转换器
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
// 创建配置类
FastJsonConfig fastJsonConfig = new FastJsonConfig();
// 修改配置返回内容的过滤
fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty);
fastConverter.setFastJsonConfig(fastJsonConfig);
// 将fastjson添加到视图消息转换器列表内
converters.add(fastConverter);
}
use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project spring-boot by Linda-Tan.
the class RestTemplateConfig method restTemplate.
@Bean("restTemplate")
@ConditionalOnMissingBean({ RestOperations.class, RestTemplate.class })
public RestTemplate restTemplate() {
// http://rensanning.iteye.com/?page=3
RestTemplate restTemplate = new RestTemplate(simpleClientHttpRequestFactory());
restTemplate.getMessageConverters().add(new FastJsonHttpMessageConverter());
return restTemplate;
}
Aggregations