content: package org.superboot.config;
  
  import com.fasterxml.jackson.core.JsonGenerator;
  import com.fasterxml.jackson.core.JsonProcessingException;
  import com.fasterxml.jackson.databind.JsonSerializer;
  import com.fasterxml.jackson.databind.ObjectMapper;
  import com.fasterxml.jackson.databind.SerializerProvider;
  import com.fasterxml.jackson.databind.module.SimpleModule;
  import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
  import com.fasterxml.jackson.databind.DeserializationFeature;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.http.converter.HttpMessageConverter;
  import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
  import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  
  import java.io.IOException;
  import java.sql.Timestamp;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import java.util.List;
  
  /**
   * <b> 序列化配置类 </b>
   * <p>
   * 功能描述:解决后台传入前端出现的long的精度丢失及日期显示为long的问题
   * </p>
   */
  @Configuration
  public class MapperConfig extends WebMvcConfigurerAdapter {
  
      @Override
      public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
          MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
          ObjectMapper objectMapper = new ObjectMapper();
          /**
           * 序列换成json时,将所有的long变成string
           * 因为js中得数字类型不能包含所有的java long值
           */
          SimpleModule simpleModule = new SimpleModule();
          //处理Long为字符串
          simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
          //处理Long类型为字符串
          simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
          //处理日期类型
          simpleModule.addSerializer(Date.class, new JsonSerializer<Date>() {
              @Override
              public void serialize(Date value, JsonGenerator jsonGenerator,
                                    SerializerProvider provider) throws IOException, JsonProcessingException {
                  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                  jsonGenerator.writeString(sdf.format(value));
              }
          });
          //处理时间戳类型
          simpleModule.addSerializer(Timestamp.class, new JsonSerializer<Date>() {
              @Override
              public void serialize(Date value, JsonGenerator jsonGenerator,
                                    SerializerProvider provider) throws IOException, JsonProcessingException {
                  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                  jsonGenerator.writeString(sdf.format(value));
              }
          });
  
          objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
          objectMapper.registerModule(simpleModule);
          jackson2HttpMessageConverter.setObjectMapper(objectMapper);
          converters.add(jackson2HttpMessageConverter);
  
  
      }
  }
  