瀏覽代碼

新增FastJson支持

zh.sqy 9 年之前
父節點
當前提交
e46cec4f09

+ 117 - 0
hsweb-web-controller/src/main/java/org/hsweb/web/controller/FastJsonHttpMessageConverter.java

@@ -0,0 +1,117 @@
+package org.hsweb.web.controller;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import com.alibaba.fastjson.serializer.PropertyPreFilter;
+import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
+import org.hsweb.web.message.ResponseMessage;
+import org.springframework.http.HttpInputMessage;
+import org.springframework.http.HttpOutputMessage;
+import org.springframework.http.MediaType;
+import org.springframework.http.converter.AbstractHttpMessageConverter;
+import org.springframework.http.converter.HttpMessageNotReadableException;
+import org.springframework.http.converter.HttpMessageNotWritableException;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.serializer.SerializerFeature;
+
+public class FastJsonHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
+
+    public final static Charset UTF8 = Charset.forName("UTF-8");
+
+    private Charset charset = UTF8;
+
+    private SerializerFeature[] features = new SerializerFeature[0];
+
+    public FastJsonHttpMessageConverter() {
+        super(new MediaType("application", "json", UTF8), new MediaType("application", "*+json", UTF8));
+    }
+
+    @Override
+    protected boolean supports(Class<?> clazz) {
+        return true;
+    }
+
+    public Charset getCharset() {
+        return this.charset;
+    }
+
+    public void setCharset(Charset charset) {
+        this.charset = charset;
+    }
+
+    public SerializerFeature[] getFeatures() {
+        return features;
+    }
+
+    public void setFeatures(SerializerFeature... features) {
+        this.features = features;
+    }
+
+    @Override
+    protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException,
+            HttpMessageNotReadableException {
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+        InputStream in = inputMessage.getBody();
+
+        byte[] buf = new byte[1024];
+        for (; ; ) {
+            int len = in.read(buf);
+            if (len == -1) {
+                break;
+            }
+
+            if (len > 0) {
+                baos.write(buf, 0, len);
+            }
+        }
+
+        byte[] bytes = baos.toByteArray();
+        return JSON.parseObject(bytes, 0, bytes.length, charset.newDecoder(), clazz);
+    }
+
+    @Override
+    protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException,
+            HttpMessageNotWritableException {
+        OutputStream out = outputMessage.getBody();
+        String text;
+        if (obj instanceof ResponseMessage) {
+            ResponseMessage message = (ResponseMessage) obj;
+            if (message.isOnlyData())
+                obj = message.getData();
+            text = JSON.toJSONString(obj, parseFilter(message), features);
+        } else {
+            text = JSON.toJSONString(obj, features);
+        }
+        byte[] bytes = text.getBytes(charset);
+        out.write(bytes);
+    }
+
+    protected PropertyPreFilter[] parseFilter(ResponseMessage responseMessage) {
+        List<PropertyPreFilter> filters = new ArrayList<>();
+        if (responseMessage.getIncludes() != null)
+            for (Map.Entry<Class<?>, Set<String>> classSetEntry : responseMessage.getIncludes().entrySet()) {
+                SimplePropertyPreFilter filter = new SimplePropertyPreFilter(classSetEntry.getKey());
+                filter.getIncludes().addAll(classSetEntry.getValue());
+                filters.add(filter);
+            }
+        if (responseMessage.getExcludes() != null)
+        for (Map.Entry<Class<?>, Set<String>> classSetEntry : responseMessage.getExcludes().entrySet()) {
+            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(classSetEntry.getKey());
+            filter.getExcludes().addAll(classSetEntry.getValue());
+            filters.add(filter);
+        }
+        return filters.toArray(new PropertyPreFilter[filters.size()]);
+    }
+
+}

+ 24 - 0
hsweb-web-controller/src/main/java/org/hsweb/web/controller/MessageConverterConfiguration.java

@@ -0,0 +1,24 @@
+package org.hsweb.web.controller;
+
+import com.alibaba.fastjson.serializer.SerializerFeature;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.converter.HttpMessageConverter;
+
+/**
+ * Created by 浩 on 2016-01-22 0022.
+ */
+@Configuration
+public class MessageConverterConfiguration {
+
+    @Bean
+    public HttpMessageConverter<Object> converter() {
+        FastJsonHttpMessageConverter converter =  new FastJsonHttpMessageConverter();
+        converter.setFeatures(SerializerFeature.WriteNullListAsEmpty);
+        converter.setFeatures(SerializerFeature.WriteNullNumberAsZero);
+        converter.setFeatures(SerializerFeature.WriteNullBooleanAsFalse);
+        converter.setFeatures(SerializerFeature.WriteDateUseDateFormat);
+
+        return converter;
+    }
+}