zoukankan      html  css  js  c++  java
  • 使用Jackson对字典翻译

    使用Jackson对字典翻译

    参考:https://juejin.cn/post/6844904053844115470

    字典项注解

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    @Documented
    public @interface DictValue {
        //字典名称
        DictEnum value();
    }
    
    

    Bean序列化更改器

    public class DictSerializerModifier extends BeanSerializerModifier {
        //这个方法在类第一次序列化时会调用一次,确定后就不会再更改了
        @Override
        public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
            for (BeanPropertyWriter beanProperty : beanProperties) {
                DictValue dictValue = beanProperty.getAnnotation(DictValue.class);
                if (dictValue != null){
                    DictFieldSerializer dictFieldSerializer = new DictFieldSerializer(dictValue.value().getName());
                    //自定以序列器
                    beanProperty.assignSerializer(dictFieldSerializer);
                    //null值序列器
                    beanProperty.assignNullSerializer(NullSerializer.instance);
                }
            }
            return beanProperties;
        }
    }
    

    自定义序列器

    
    public class DictFieldSerializer extends JsonSerializer<Object> {
    
        private String key;
    
        public DictFieldSerializer(String key){
            this.key = key;
        }
    
        //自定义写入方法
        @Override
        public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            IDictService dictService = SpringTool.getBean(IDictService.class);
            String dictValue = dictService.getDictValue(key, value.toString());
            gen.writeString(dictValue);
        }
    }
    
    

    SpringBoot配置

    
    @Configuration
    public class JacksonConfig {
    
    
        @Bean
        public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
            ObjectMapper objectMapper = builder.createXmlMapper(false).build();
            objectMapper.setSerializerFactory(objectMapper.getSerializerFactory().withSerializerModifier(new DictSerializerModifier()));
            return objectMapper;
        }
    }
    
    
  • 相关阅读:
    BZOJ 2724: [Violet 6]蒲公英
    codeforces Lightsabers (hard)
    BZOJ 3884: 上帝与集合的正确用法
    BZOJ 4809: 皇后
    python的变量类型(Day6)
    Python集合方法整理(Day9)
    基本运算符与流程控制(Day5)
    基本数据类型(Day4)
    第一个Python程序(Day3)
    操作系统(Day2.5)
  • 原文地址:https://www.cnblogs.com/huisunan/p/14808551.html
Copyright © 2011-2022 走看看