Ver Fonte

JSON序列化考虑Lazy Fetch

yingp há 8 anos atrás
pai
commit
a78a58818a

+ 4 - 8
pom.xml

@@ -9,7 +9,6 @@
 		<groupId>com.uas.platform</groupId>
 		<artifactId>platform</artifactId>
 		<version>0.0.1-SNAPSHOT</version>
-		<relativePath>../platform</relativePath>
 	</parent>
 	<profiles>
 		<profile>
@@ -350,12 +349,10 @@
 					</execution>
 				</executions>
 				<configuration>
-					<configuration>
-						<format>{0,date,yyyy-MM-dd HH:mm:ss}</format>
-						<items>
-							<item>timestamp</item>
-						</items>
-					</configuration>
+					<format>{0,date,yyyy-MM-dd HH:mm:ss}</format>
+					<items>
+						<item>timestamp</item>
+					</items>
 				</configuration>
 			</plugin>
 			<plugin>
@@ -449,7 +446,6 @@
 								<sourceInclude>resources/tpl/**/*.html</sourceInclude>
 								<sourceInclude>WEB-INF/views/**/*.html</sourceInclude>
 							</sourceIncludes>
-							http://static.ubtob.com/css/index.css?_v=1450321871828
 							<versionSuffix>
 								<suffix>?_v=${timestamp}</suffix>
 								<exclude>*/require.js,*.min.js,*.min.css</exclude>

+ 34 - 0
src/main/java/com/uas/platform/b2b/core/serializer/LazyPropertyFilter.java

@@ -0,0 +1,34 @@
+package com.uas.platform.b2b.core.serializer;
+
+import com.alibaba.fastjson.serializer.PropertyFilter;
+import org.hibernate.collection.spi.PersistentCollection;
+import org.hibernate.proxy.HibernateProxy;
+import org.hibernate.proxy.LazyInitializer;
+
+/**
+ * Created by Pro1 on 2017/11/13.
+ *
+ * JSON序列化时,过滤Hibernate代理对象或者延迟加载的对象FetchType.LAZY
+ */
+public class LazyPropertyFilter implements PropertyFilter{
+
+    @Override
+    public boolean apply(Object object, String name, Object value) {
+        if (value instanceof HibernateProxy) {//hibernate代理对象
+            LazyInitializer initializer = ((HibernateProxy) value).getHibernateLazyInitializer();
+            if (initializer.isUninitialized()) {
+                return false;
+            }
+        } else if (value instanceof PersistentCollection) {//实体关联集合一对多等
+            PersistentCollection collection = (PersistentCollection) value;
+            if (!collection.wasInitialized()) {
+                return false;
+            }
+            Object val = collection.getValue();
+            if (val == null) {
+                return false;
+            }
+        }
+        return true;
+    }
+}

+ 16 - 5
src/main/webapp/WEB-INF/spring/webmvc.xml

@@ -7,6 +7,21 @@
 	<!-- 开启@AspectJ AOP代理 -->
 	<aop:aspectj-autoproxy />
 
+	<bean id="lazyPropertyFilter" class="com.uas.platform.b2b.core.serializer.LazyPropertyFilter"></bean>
+
+	<bean id="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig">
+		<property name="serializerFeatures">
+			<list>
+				<value>DisableCircularReferenceDetect</value>
+			</list>
+		</property>
+		<property name="serializeFilters">
+			<list>
+				<ref bean="lazyPropertyFilter"/>
+			</list>
+		</property>
+	</bean>
+
 	<mvc:annotation-driven>
 		<mvc:message-converters register-defaults="true">
 			<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
@@ -17,11 +32,7 @@
 						<value>application/json;charset=UTF-8</value>
 					</list>
 				</property>
-				<property name="features">
-					<list>
-						<value>DisableCircularReferenceDetect</value>
-					</list>
-				</property>
+				<property name="fastJsonConfig" ref="fastJsonConfig" />
 			</bean>
 		</mvc:message-converters>
 	</mvc:annotation-driven>