java枚举类型JSON格式返回处理

public enum  PositionParam {
    POSITION_PARAM1(1,"总经理"),
    POSITION_PARAM2(2,"销售经理"),
    POSITION_PARAM3(3,"人力资源");

    private Integer code;
    private String name;

    private PositionParam(Integer code,String name){
        this.code=code;
        this.name=name;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

上面的枚举类以JSON格式返给前台后显示的是

["POSITION_PARAM1","POSITION_PARAM2","POSITION_PARAM2"]

而实际想要得到的数据是

[{name:"总经理",code:1},{name:"销售经理",code:2},{name:"人力资源",code:3}]

处理办法:

引入jar依赖


    com.alibaba
    fastjson
    1.2.60

然后在枚举类中加入以下注解

@JsonFormat(shape = JsonFormat.Shape.OBJECT)

你可能感兴趣的:(web知识)