springboot学习(二十六) springboot中自定义枚举类型的序列化和反序列化

1、使用@JsonCreator 反序列化
定义一个通过某个字段生成枚举的函数,并添加@JsonCreator注解。这样在前端传入int类型会转为枚举类型。

    @JsonCreator
    public static SexEnum getByCode(int code) {
        for (SexEnum value : SexEnum.values()) {
            if (Objects.equals(code, value.getCode())) {
                return value;
            }
        }
        return null;
    }

2、使用@JsonValue序列化
枚举内某个字段的getter方法上添加@JsonValue注解。这样在返回前端数据时,会将枚举类型转为int类型的code的值。

    @JsonValue
    public int getCode() {
        return this.code;
    }

3、完整的枚举

package com.iscas.biz.mp.test.model.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.annotation.JsonView;
import com.iscas.biz.mp.test.model.TestEntity;
import lombok.Getter;

import java.util.Objects;

/**
 * //TODO
 *
 * @author zhuquanwen
 * @vesion 1.0
 * @date 2020/4/5 15:23
 * @since jdk1.8
 */

public enum  SexEnum /*implements IEnum<Integer>*/ {

    /**
     * 男
     * */
    MAN(1, "男"),

    /**
     * 女
     * */
    WOMEN(2, "女");


    @EnumValue
    private final int code;

    @JsonValue
    public int getCode() {
        return this.code;
    }

    public String getDescription() {
        return description;
    }

    private final String description;
    SexEnum(int val, String description) {
        this.code = val;
        this.description = description;
    }

    @JsonCreator
    public static SexEnum getByCode(int code) {
        for (SexEnum value : SexEnum.values()) {
            if (Objects.equals(code, value.getCode())) {
                return value;
            }
        }
        return null;
    }
/*
    @Override
    public Integer getValue() {
        return code;
    }*/
}

package com.iscas.biz.mp.test.model;

import com.iscas.biz.mp.test.model.enums.SexEnum;
import lombok.Data;

/**
 * //TODO
 *
 * @author zhuquanwen
 * @vesion 1.0
 * @date 2020/4/5 15:22
 * @since jdk1.8
 */
@Data
public class TestEntity {
    private String name;

    private SexEnum sex;
}


http://www.niftyadmin.cn/n/1302418.html

相关文章

springboot学习(二十七) @ConditionalOnProperty的使用

ConditionalOnProperty一般加在Configurarion、Component配置的类上或Bean配置的方法上&#xff0c;表示满足获取到某些配置文件信息后才会配置或加载。 ConditionalOnProperty源码如下: Retention(RetentionPolicy.RUNTIME) Target({ElementType.TYPE, ElementType.METHOD}) …

解决IDEA中gradle打包或执行命令时中文乱码

在我们使用gradle打包时经常出现中文提示的乱码&#xff0c;解决办法很简单&#xff0c;设置一下idea的编码&#xff1a; 在help-Edit Custom VM Options下添加一行 -Dfile.encodingUTF-8

解决linux下使用root用户启动nexus报错Detected execution as root user. This is NOT recommended!的问题

在linux部署nexus使用root启动会报错Detected execution as “root” user. This is NOT recommended! 处理这个问题很容易&#xff0c;找到nexus-3.16.1-02/bin下nexus文件&#xff0c;vi编辑&#xff0c;将run_as_rootfalse改为run_as_roottrue&#xff0c;再次启动就可以了。…

Mybatis-plus大数据量数据流式查询通用接口

1、定义一个通用mapper Repository public interface DynamicMapper extends BaseMapper {Select("${sql}" )List<Map> dynamicSelect(Param("sql") String sql);Insert("${sql}")void dynamicInsert(Param("sql") String sql)…

mybatis的使用及源码分析(六) mybatis自定义plugin

使用Mybatis的时候&#xff0c;会使用到各种插件&#xff0c;如PageHelper&#xff08;分页插件&#xff09;等&#xff0c;下面介绍自定义plugin的方法 Mybatis插件又称拦截器&#xff0c;Mybatis采用责任链模式&#xff0c;通过动态代理组织多个插件&#xff08;拦截器&#…

Mysql 8.0.19无法定位程序输入点terminate于动态链接库C:\windows\System32\VCRUNTIME140_1.dll

Mysql 8.0.19无法定位程序输入点terminate于动态链接库C:\windows\System32\VCRUNTIME140_1.dll注&#xff1a;先更新服务在新搭建的服务器上安装环境需要注意&#xff1a; 自己在新搭建好的服务器上安装环境&#xff0c;报了该错误&#xff0c;百度了一下午&#xff0c;各种方…

springboot学习(二十八)springboot开启gzip压缩

在properties配置文件内加入配置 server.compression.enabledtrue server.compression.mime-typesapplication/json,application/xml,text/html,text/xml,text/plain

精确浮点运算工具类以及BigDecimal.valueOf(double d)与new BigDecimal(double d)的区别

工具类如下&#xff1a; package com.iscas.common.tools.core.arithmetic;import java.math.BigDecimal; import java.math.RoundingMode;/*** 浮点数精确运算工具类** 使用BigDecimal代替直接浮点运算&#xff0c;在大量数据计算时效率低&#xff0c;谨慎使用** author zhuq…