package com.sptg.gateway.cfgbeans;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.sptg.common.core.filter.EncryptionFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * title: WebConfigBean
 * package: com.sptg.gateway.cfgbeans
 * description: Web配置类
 *
 *                  
 *               
 */
@Configuration
public class WebConfigBean {

//    /**
//     * 功能描述: 配置加解密过滤器
//     * @return FilterRegistrationBean
//       *                  
//       *                     
//     */
//    @Bean
//    public FilterRegistrationBean encryptionFilter(){
//        EncryptionFilter encryptionFilter = new EncryptionFilter();
//        //  设置不需要拦截的路径
//        List<String> excludeList = new ArrayList<>(1);
//        excludeList.add("/common/file/upload");
//        excludeList.add("/common/file/uploadBase64");
//        excludeList.add("/actuator/hystrix.stream");
//        encryptionFilter.setExcludeList(excludeList);
//        FilterRegistrationBean<EncryptionFilter> registration = new FilterRegistrationBean<>(encryptionFilter);
//        //  拦截全部请求
//        registration.addUrlPatterns("/*");
//        //  设置过滤器名称
//        registration.setName(encryptionFilter.getClass().getSimpleName());
//        return registration;
//    }

    @Bean
    public ObjectMapper serializingObjectMapper(Jackson2ObjectMapperBuilder builder){
        ObjectMapper mapper = builder.createXmlMapper(false).build();
        //  禁用对日期以时间戳方式输出的特性
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        mapper.registerModule(new JavaTimeModule());
        //  忽略value为null时，key的输出
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        /*
            序列换成json时,将所有的long变成string
            因为js中得数字类型不能包含所有的java long值
         */
        SimpleModule module = new SimpleModule();
        module.addSerializer(Long.class, ToStringSerializer.instance);
        module.addSerializer(Long.TYPE, ToStringSerializer.instance);
        mapper.registerModule(module);
        return mapper;
    }
}
