@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); } } }; }
@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); }
@Bean public FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter4() { FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4(); List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.APPLICATION_JSON_UTF8); converter.setSupportedMediaTypes(mediaTypes); return converter; }
@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; }
@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); }
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); }
@Override public boolean supports(MethodParameter returnType, Class converterType) { return FastJsonHttpMessageConverter4.class.isAssignableFrom(converterType); }