本文整理汇总了Java中com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4类的典型用法代码示例。如果您正苦于以下问题:Java FastJsonHttpMessageConverter4类的具体用法?Java FastJsonHttpMessageConverter4怎么用?Java FastJsonHttpMessageConverter4使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
FastJsonHttpMessageConverter4类属于com.alibaba.fastjson.support.spring包,在下文中一共展示了FastJsonHttpMessageConverter4类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: customJackson2HttpMessageConverter
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4; //导入依赖的package包/类
@Bean
public HttpMessageConverter customJackson2HttpMessageConverter() {
return new FastJsonHttpMessageConverter4() {
@Override
protected void writeInternal(Object obj, Type type, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
if (obj instanceof ResultModel) {
super.writeInternal(obj, type, outputMessage);
} else {
super.writeInternal(ResultResolver.sendNormalResult(obj), type, outputMessage);
}
}
};
}
示例2: configureMessageConverters
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4; //导入依赖的package包/类
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.WriteMapNullValue,//保留空的字段
SerializerFeature.WriteNullStringAsEmpty,//String null -> ""
SerializerFeature.WriteNullNumberAsZero);//Number null -> 0
converter.setFastJsonConfig(config);
converter.setDefaultCharset(Charset.forName("UTF-8"));
converters.add(converter);
}
示例3: fastJsonHttpMessageConverter4
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4; //导入依赖的package包/类
@Bean
public FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter4() {
FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
converter.setSupportedMediaTypes(mediaTypes);
return converter;
}
示例4: fastJsonHttpMessageConverter
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4; //导入依赖的package包/类
@Bean
@ConditionalOnMissingBean({ FastJsonHttpMessageConverter4.class })
//当没有注册这个类时,自动注册Bean
public FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter() {
FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
//使用最新的官方推荐配置对象的方式来注入fastjson的序列化特征
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.WriteDateUseDateFormat,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.WriteNullStringAsEmpty
);
//添加对json值的过滤,因为像移动APP,服务端在传json值时最好不要传null,而是使用“”,这是一个演示
ValueFilter valueFilter = new ValueFilter() {//5
//o 是class
//s 是key值
//o1 是value值
public Object process(Object o, String s, Object o1) {
if (null == o1) {
o1 = "";
}
return o1;
}
};
fastJsonConfig.setSerializeFilters(valueFilter);
converter.setFastJsonConfig(fastJsonConfig);
return converter;
}
示例5: registry
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4; //导入依赖的package包/类
@Override
public void registry(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter4();
ArrayList<MediaType> arrayList = new ArrayList<MediaType>() {{
add(MediaType.APPLICATION_JSON_UTF8);
add(MediaType.valueOf("text/html;charset=UTF-8"));
add(MediaType.MULTIPART_FORM_DATA);
}};
fastJsonHttpMessageConverter.setSupportedMediaTypes(arrayList);
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.QuoteFieldNames,
SerializerFeature.DisableCircularReferenceDetect);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
converters.add(0, fastJsonHttpMessageConverter);
}
示例6: test_1
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4; //导入依赖的package包/类
public void test_1() throws Exception {
FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
Assert.assertNotNull(converter.getFastJsonConfig());
converter.setFastJsonConfig(new FastJsonConfig());
converter.canRead(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
converter.canWrite(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
Method method1 = FastJsonHttpMessageConverter4.class.getDeclaredMethod(
"supports", Class.class);
method1.setAccessible(true);
method1.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, 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"));
Method method2 = FastJsonHttpMessageConverter4.class.getDeclaredMethod(
"readInternal", Class.class, HttpInputMessage.class);
method2.setAccessible(true);
method2.invoke(converter, VO.class, input);
}
示例7: supports
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4; //导入依赖的package包/类
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
return FastJsonHttpMessageConverter4.class.isAssignableFrom(converterType);
}