SpringBoot 接口对数据枚举类型的入参以及出参转换处理

1、在项目中使用枚举类型

一般我们在项目中,设置表的类型字段,都是用int存储,用0、1、2…代表不同类型的含义。
以性别字段为例:
数据库表字段设计为integer:

CREATE TABLE t_user_info
(
	...
    sex INT NOT NULL COMMENT '性别,0-女,1-男'
    ...
);

实体成员变量设计为枚举:

public class UserInfoEntity extends BaseField {
   
	...
    /** 性别,0-女,1-男 */
    private Sex sex;
    ...
}

@Getter
public enum Sex implements BaseEnum {
   
    woman(0, "女"), man(1, "男");

    @EnumValue //标记数据库存的值是sex
    private Integer code;

    private String desc;

    Sex(int code, String desc) {
   
        this.code = code;
        this.desc = desc;
    }

    public String getStrCode() {
   
        return code.toString();
    }
}

2、不做任何处理的演示效果

如果我们不做任何额外处理,当使用Sex枚举作为出入参时。只能返回manwoman或用manwoman作为参数。

2.1、接口出参

封装出参实体中sex成员变量类型为Sex枚举,则出参为:manwoman
在这里插入图片描述

2.2、接口入参

若是使用Sex枚举类型接收请求参数,不论是从接口get请求url中获取,还是使用构造实体接收post请求参数。都只能用manwoman作为参数,如果使用0或1作为参数,则无法正确被转换为Sex枚举。但是在开发过程中前端下拉选选项一般都与0,1,2…相对应。
在这里插入图片描述

在这里插入图片描述

3、用枚举的code作为参数和返回值

以上面的Sex枚举为例子,我希望在返回sex字段时,用0和1作为返回值,并且希望能用0和1映射男和女作为接口入参。该如何实现呢?

3.1 代码案例

3.1.1、定义枚举基础接口BaseEnum,每个枚举都实现该接口

/**
 * 枚举基础接口
 * 注意:不要构造枚举name和code相互交叉使用的枚举,否则在进行转换时将获取意外的结果。例如:
 *public enum Sex2 implements BaseEnum {
 *     woman("man", "女"), man("woman", "男");
 *
 *     private String code;
 *     private String desc;
 *
 *     Sex2(String code, String desc) {
 *         this.code = code;
 *         this.desc = desc;
 *     }
 *     public String getStrCode() {
 *         return code;
 *     }
 * }
 *
 * 若是这种则是可以的:woman("woman", "女"), man("man", "男");
 */
public interface BaseEnum {
   

    /**
     * 根据枚举值或名称从枚举类型type中获取枚举对象
     */
    public static <T extends BaseEnum> T getEnum(Class<T> type, Object codeOrName) {
   
        T[] enums = type.getEnumConstants();
        for (T em : enums) {
   
            if (em.getStrCode().equals(codeOrName.toString()) || em.name().equals(codeOrName.toString())) {
   
                return em;
            }
        }
        return null;
    }

    /**
     * 获取枚举值的字符串code
     *
     * @return 编码
     */
    String getStrCode();

    /**
     * 获取枚举名称
     *
     * @return 名称
     */
    String name();
}

3.1.2、性别Sex枚举并实现接口BaseEnum

import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.Getter;

@Getter
public enum Sex implements BaseEnum {
   
    woman(0, "女"), man(1, "男");

    @EnumValue //标记数据库存的值是sex
    private Integer code;
    private String desc;
	
    Sex(int code, String desc) {
   
        this.code = code;
        this.desc = desc;
    }

    public String getStrCode() {
   
        return code.toString();
    }
}

3.1.3、定义BaseEnum枚举接口序列化

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.study.enums.BaseEnum;
import java.io.IOException;

/**
 * BaseEnum 序列化
 */
public class BaseEnumSerializer extends JsonSerializer<BaseEnum> {
   
    @Override
    public void serialize(BaseEnum value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
   
        gen.writeNumber(value.getStrCode());
        // 增加一个字段,格式为【枚举类名称+Text】,存储枚举的name
        gen.writeStringField(gen.getOutputContext().getCurrentName() + "Text", value.name());
    }
}

3.1.4、自定义Enum枚举接口反序列化

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.study.enums.BaseEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import java.io.IOException;

/**
 * BaseEnum反序列化
 */
@Slf4j
public class BaseEnumDeserializer extends JsonDeserializer<Enum> {
   
    @Override
    public Enum deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
   
        JsonNode node = p.getCodec().readTree(p);
        String currentName = p.currentName();
        Object currentValue = p.getCurrentValue();
        Class findPropertyType = BeanUtils.findPropertyType(currentName, currentValue.getClass());
        if (findPropertyType == null) {
   
            log.info("在" + currentValue.getClass() + "实体类中找不到" + currentName + "字段");
            return null;
        }

        String asText = node.asText();
        if (StringUtils.isBlank(asText)) {
   
            return null;
        }

        if (BaseEnum.class.isAssignableFrom(findPropertyType)) {
   
            BaseEnum valueOf = null;
            if (StringUtils.isNotBlank(asText)) {
   
                valueOf = BaseEnum.getEnum(findPropertyType, asText);
            }
            if (valueOf != null) {
   
                return (Enum) valueOf;
            }
        }
        return Enum.valueOf(findPropertyType, asText);
    }
}

3.1.5、配置自定义的BaseEnum类的序列化和Enum类的反序列

配置Jackson在序列化BaseEnum和反序列化Enum时,使用我们自己定义的BaseEnumSerializer、EnumDeserializer进行处理。

因为Spring boot使用的是Jackson序列化接口出参的,所以若我们自定定义的枚举若是都实现了BaseEnum接口,那么就会调用BaseEnumSerializer序列化枚举对象,达到预期的效果。

package com.study.config;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.study.enums.BaseEnum;
import com.study.serializer.EnumDeserializer;
import com.study.serializer.BaseEnumSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.TimeZone;

/**
 * 加载baseEnum 构建bean定义,初始化Spring容器。
 */
@Configuration
public class JacksonConfig {
   
    // JacksonConfig EnumBeanConfig
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization() {
   
        BaseEnumSerializer baseEnumSerializer = new BaseEnumSerializer();
        EnumDeserializer enumDeserializer = new EnumDeserializer();
        return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.timeZone(TimeZone.getDefault())
                // 配置Jackson在序列化BaseEnum和反序列化Enum时,使用我们自己定义的BaseEnumSerializer、EnumDeserializer进行处理
                .serializerByType(BaseEnum.class, baseEnumSerializer)
                .deserializerByType(Enum.class, enumDeserializer);
    }
}

3.1.6、配置类型转换工厂IntegerCodeToEnumConverterFactory和StringCodeToEnumConverterFactory

作用是当我们使用枚举类型接收接口入参时,会根据请求参数是Integer或者String调用各自的转换器转为BaseEnum枚举类型

import com.study.converters.IntegerToEnumConverter;
import com.study.enums.BaseEnum;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import java.util.HashMap;
import java.util.Map;

/**
 * 枚举 转化器工厂类
 * 将Integer类型的参数转换为BaseEnum类型枚举对象
 * 参考:org.springframework.core.convert.support.IntegerToEnumConverterFactory
 */
public class IntegerCodeToEnumConverterFactory implements ConverterFactory<Integer, BaseEnum> {
   
    private static final Map<Class, Converter> CONVERTERS = new HashMap<>();

    /**
     * 获取一个从 Integer 转化为 T 的转换器,T 是一个泛型,有多个实现
     *
     * @param targetType 转换后的类型
     * @return 返回一个转化器
     */
    @Override
    public <T extends BaseEnum> Converter<Integer, T> getConverter(Class<T> targetType) {
   
        Converter<Integer, T> converter = CONVERTERS.get(targetType);
        if (converter == null) {
   
            converter = new IntegerToEnumConverter<>(targetType);
            CONVERTERS.put(targetType, converter);
        }
        return converter;
    }
}
import cn.hutool.core.util.ObjectUtil;
import com.study.enums.BaseEnum;
import org.springframework.core.convert.converter.Converter;
import java.util.HashMap;
import java.util.Map;

/**
 * Integer类型参数枚举 转化器
 *
 * @param <T>
 */
public class IntegerToEnumConverter<T extends BaseEnum> implements Converter<Integer, T> {
   
    private Map<String, T> enumMap = new HashMap<>();

    public IntegerToEnumConverter(Class<T> enumType) {
   
        T[] enums = enumType.getEnumConstants();
        for (T e : enums) {
   
            enumMap.put(e.getStrCode(), e);
        }
    }

    @Override
    public T convert(Integer source) {
   
        T t = enumMap.get(source);
        if (ObjectUtil.isNull(t)) {
   
            throw new IllegalArgumentException("无法匹配对应的枚举类型");
        }
        return t;
    }
}
import com.study.converters.StringToEnumConverter;
import com.study.enums.BaseEnum;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import java.util.HashMap;
import java.util.Map;

/**
 * 枚举 转化器工厂类
 * 将String类型的参数转换为BaseEnum类型枚举对象
 * 参考:org.springframework.core.convert.support.StringToEnumConverterFactory
 */
public class StringCodeToEnumConverterFactory implements ConverterFactory<String, BaseEnum> {
   
    private static final Map<Class, Converter> CONVERTERS = new HashMap<>();

    /**
     * 获取一个从 Integer 转化为 T 的转换器,T 是一个泛型,有多个实现
     *
     * @param targetType 转换后的类型
     * @return 返回一个转化器
     */
    @Override
    public <T extends BaseEnum> Converter<String, T> getConverter(Class<T> targetType) {
   
        Converter<String, T> converter = CONVERTERS.get(targetType);
        if (converter == null) {
   
            converter = new StringToEnumConverter<>(targetType);
            CONVERTERS.put(targetType, converter);
        }
        return converter;
    }
}
import cn.hutool.core.util.ObjectUtil;
import com.study.enums.BaseEnum;
import org.springframework.core.convert.converter.Converter;
import java.util.HashMap;
import java.util.Map;

/**
 * String类型参数枚举 转化器
 *
 * @param <T>
 */
public class StringToEnumConverter<T extends BaseEnum> implements Converter<String, T> {
   
    private final Map<String, T> enumMap = new HashMap<>();

    public StringToEnumConverter(Class<T> enumType) {
   
        T[] enums = enumType.getEnumConstants();
        for (T e : enums) {
   
            enumMap.put(e.getStrCode(), e);
            enumMap.put(e.name(), e);
        }
    }

    @Override
    public T convert(String source) {
   
        T t = enumMap.get(source);
        if (ObjectUtil.isNull(t)) {
   
            throw new IllegalArgumentException("无法匹配对应的枚举类型");
        }
        return t;
    }
}

3.1.7、将转换工厂注册到Spring MVC中

import com.study.converters.factory.IntegerCodeToEnumConverterFactory;
import com.study.converters.factory.StringCodeToEnumConverterFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfigurer implements WebMvcConfigurer {
   
    /**
     * 枚举类的转换器工厂 addConverterFactory
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
   
        registry.addConverterFactory(new IntegerCodeToEnumConverterFactory());
        registry.addConverterFactory(new StringCodeToEnumConverterFactory());
    }
}

3.2 演示效果

使用0、1、man、woman都可以请求接口
在这里插入图片描述
接口返回值为0、1。还新增了字段sexText保存枚举的名称
在这里插入图片描述

4、项目代码地址

项目gitee链接: https://gitee.com/WillDistance/mybatis-plus-h2.git

相关推荐

  1. SpringBoot-打印请求

    2023-12-30 17:22:03       21 阅读
  2. TS设置接收类型

    2023-12-30 17:22:03       7 阅读
  3. 【Django】类型数据

    2023-12-30 17:22:03       16 阅读
  4. SpringBoot实现类型参数认证

    2023-12-30 17:22:03       38 阅读
  5. mybatis 以及plus 处理

    2023-12-30 17:22:03       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-30 17:22:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-30 17:22:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-30 17:22:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-30 17:22:03       20 阅读

热门阅读

  1. 百度编辑器常用设置

    2023-12-30 17:22:03       37 阅读
  2. 工智能基础知识总结--什么是AdaBoost

    2023-12-30 17:22:03       34 阅读
  3. 基于遗传算法的双层规划,基于ga的双层规划

    2023-12-30 17:22:03       44 阅读
  4. Linux ubuntu 设置固定IP以及DNS

    2023-12-30 17:22:03       35 阅读
  5. 函数调用图生成_incomplete

    2023-12-30 17:22:03       37 阅读
  6. mysql隔离级别和串行化

    2023-12-30 17:22:03       40 阅读
  7. LeetCode每日一题——1185. Day of the Week

    2023-12-30 17:22:03       36 阅读
  8. 函数的地址

    2023-12-30 17:22:03       36 阅读