All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gccloud.starter.common.configuration.ObjectMapperConfiguration Maven / Gradle / Ivy

package com.gccloud.starter.common.configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
import com.gccloud.starter.common.constant.GlobalConst;
import com.gccloud.starter.common.entity.ExtendObj;
import com.gccloud.starter.common.json.ExtendObjDeserializer;
import com.gccloud.starter.common.json.ExtendObjSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Slf4j
@Configuration
@ConditionalOnProperty(prefix = "gc.starter.component", name = "ObjectMapperConfiguration", havingValue = "ObjectMapperConfiguration", matchIfMissing = true)
public class ObjectMapperConfiguration {

    @Resource
    private ObjectMapper objectMapper;

    @PostConstruct
    public void init() {
        log.info(GlobalConst.Console.LINE);
        log.info("注册 Jackson JsonOrgModule 模块");
        // 不注册该模块会导致 @RequestBody 为 JSONObject 时属性无法填充
        JsonOrgModule jsonOrgModule = new JsonOrgModule();
        objectMapper.registerModule(jsonOrgModule);

        SimpleModule simpleModule = new SimpleModule();
        // 解决 ExtendObj 作为属性无法解析
        simpleModule.addSerializer(ExtendObj.class, ExtendObjSerializer.INSTANCE);
        simpleModule.addDeserializer(ExtendObj.class, ExtendObjDeserializer.INSTANCE);
        objectMapper.registerModule(simpleModule);

        log.info(GlobalConst.Console.LINE);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy