赞
踩
- <!--hutool工具包-->
- <dependency>
- <groupId>cn.hutool</groupId>
- <artifactId>hutool-all</artifactId>
- <version>5.4.2</version>
- </dependency>
- <!-- Fastjson序列化工具 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.47</version>
- </dependency>
具体步骤请查看以下博客。
全局序列化配置类。
- package com.config;
-
- import com.alibaba.fastjson.serializer.SerializeConfig;
- import com.alibaba.fastjson.serializer.SerializerFeature;
- import com.alibaba.fastjson.support.config.FastJsonConfig;
- import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
- import com.serializer.CustomCollectionSerializer;
- import com.serializer.CustomDateSerializer;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.http.MediaType;
- import org.springframework.http.converter.HttpMessageConverter;
- import org.springframework.web.servlet.config.annotation.EnableWebMvc;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
-
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
-
- /**
- * 全局序列化配置类
- */
- @Configuration
- @EnableWebMvc
- public class JsonConfig implements WebMvcConfigurer {
- /**
- * 全局序列化方式
- *
- * @param converters
- */
- @Override
- public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
- //fastjson的全局序列化方式
- configureFastJsonHttpMessageConverter(converters);
- }
-
- /**
- * fastjson的全局序列化方式
- *
- * @param converters
- */
- private void configureFastJsonHttpMessageConverter(List<HttpMessageConverter<?>> converters) {
- // 定义一个convert转换消息的对象;
- FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
- //配置Content-Type
- List<MediaType> supportedMediaTypes = new ArrayList<>();
- supportedMediaTypes.add(MediaType.APPLICATION_JSON);
- supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
- supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
- supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
- supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
- supportedMediaTypes.add(MediaType.APPLICATION_PDF);
- supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
- supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
- supportedMediaTypes.add(MediaType.APPLICATION_XML);
- supportedMediaTypes.add(MediaType.IMAGE_GIF);
- supportedMediaTypes.add(MediaType.IMAGE_JPEG);
- supportedMediaTypes.add(MediaType.IMAGE_PNG);
- supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
- supportedMediaTypes.add(MediaType.TEXT_HTML);
- supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
- supportedMediaTypes.add(MediaType.TEXT_PLAIN);
- supportedMediaTypes.add(MediaType.TEXT_XML);
- fastConverter.setSupportedMediaTypes(supportedMediaTypes);
- // 添加fastjson的配置信息
- FastJsonConfig fastJsonConfig = new FastJsonConfig();
- fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,
- SerializerFeature.WriteMapNullValue,
- SerializerFeature.WriteNullStringAsEmpty,
- SerializerFeature.WriteNullListAsEmpty,
- SerializerFeature.DisableCircularReferenceDetect,
- SerializerFeature.WriteNonStringKeyAsString
- );
- SerializeConfig serializeConfig = SerializeConfig.globalInstance;
- serializeConfig.put(Long.class, com.alibaba.fastjson.serializer.ToStringSerializer.instance);
- serializeConfig.put(Long.TYPE, com.alibaba.fastjson.serializer.ToStringSerializer.instance);
- serializeConfig.put(Date.class, new CustomDateSerializer());
- serializeConfig.put(ArrayList.class, new CustomCollectionSerializer());
- fastJsonConfig.setSerializeConfig(serializeConfig);
- // convert添加配置信息.
- fastConverter.setFastJsonConfig(fastJsonConfig);
- // 将convert添加到converters当中.
- converters.add(fastConverter);
- }
- }
自定义Date序列化类。
- package com.serializer;
-
- import cn.hutool.core.date.DateUtil;
- import com.alibaba.fastjson.serializer.DateCodec;
- import com.alibaba.fastjson.serializer.JSONSerializer;
- import com.alibaba.fastjson.serializer.SerializeWriter;
-
- import java.io.IOException;
- import java.lang.reflect.Type;
- import java.util.Date;
-
- /**
- * 自定义Date序列化类
- */
- public class CustomDateSerializer extends DateCodec {
- /**
- * 序列化方法(重写)
- *
- * @param serializer
- * @param object
- * @param fieldName
- * @param fieldType
- * @param features
- * @throws IOException
- */
- public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
- SerializeWriter out = serializer.getWriter();
- if (object == null) {
- out.writeNull();
- return;
- }
- //指定日期格式
- out.writeString(DateUtil.format((Date) object, "yyyy-MM-dd HH:mm:ss"));
- return;
- }
- }
自定义集合序列化类。
- package com.serializer;
-
- import com.alibaba.fastjson.serializer.*;
- import com.alibaba.fastjson.util.TypeUtils;
-
- import java.io.IOException;
- import java.lang.reflect.Type;
- import java.util.*;
-
- /**
- * 自定义集合序列化类
- */
- public class CustomCollectionSerializer extends CollectionCodec {
- /**
- * 序列化方法(重写)
- *
- * @param serializer
- * @param object
- * @param fieldName
- * @param fieldType
- * @param features
- * @throws IOException
- */
- public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
- SerializeWriter out = serializer.out;
-
- if (object == null) {
- out.writeNull(SerializerFeature.WriteNullListAsEmpty);
- return;
- }
-
- Type elementType = null;
- if (out.isEnabled(SerializerFeature.WriteClassName)
- || SerializerFeature.isEnabled(features, SerializerFeature.WriteClassName)) {
- elementType = TypeUtils.getCollectionItemType(fieldType);
- }
-
- Collection<?> collection = (Collection<?>) object;
-
- SerialContext context = serializer.getContext();
- serializer.setContext(context, object, fieldName, 0);
-
- if (out.isEnabled(SerializerFeature.WriteClassName)) {
- if (HashSet.class == collection.getClass()) {
- out.append("Set");
- } else if (TreeSet.class == collection.getClass()) {
- out.append("TreeSet");
- }
- }
-
- try {
- int i = 0;
- out.append('[');
- for (Object item : collection) {
-
- if (i++ != 0) {
- out.append(',');
- }
-
- if (item == null) {
- out.writeNull();
- continue;
- }
-
- Class<?> clazz = item.getClass();
- ObjectSerializer itemSerializer = serializer.getObjectWriter(clazz);
- if (SerializerFeature.isEnabled(features, SerializerFeature.WriteClassName)
- && itemSerializer instanceof JavaBeanSerializer) {
- JavaBeanSerializer javaBeanSerializer = (JavaBeanSerializer) itemSerializer;
- javaBeanSerializer.writeNoneASM(serializer, item, i - 1, elementType, features);
- } else {
- itemSerializer.write(serializer, item, i - 1, elementType, features);
- }
- }
- out.append(']');
- } finally {
- serializer.setContext(context);
- }
- }
- }
- package com.controller;
-
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
-
- @RestController
- public class JsonController {
- /**
- * 测试FastJson实现全局序列化配置
- *
- * @return
- */
- @GetMapping("/testFastJson")
- public List<Object> testFastJson() {
- List<Object> list = new ArrayList<>();
- list.add(1440931124753108994L);
- list.add(new Date());
- return list;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。