JAVA返回枚举类到前端

在实际开发过程中,常需要定义一个常量来指向一个值,这时可使用枚举类来定义常量关联属性值。当需要将枚举值返回给前端时,通过在枚举类中添加@JsonFormat(shape = JsonFormat.Shape.OBJECT)注解实现枚举类的响应。

package com.example.gupaop6study.myenum;

import com.fasterxml.jackson.annotation.JsonFormat;

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum SwitchEnum {
    ON(1, "开启"),
    OFF(0, "关闭"),
    ;

    private int code;
    private String desc;

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

    public int getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }
}
posted @ 2022-11-25 14:45  liong01  阅读(2827)  评论(0)    收藏  举报