在枚举类上添加 @JsonFormat(shape = JsonFormat.Shape.OBJECT) 然后接口返回Enums.values();就可以
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
@Getter
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum OperationTypeEnum {
LOGIN("login", "登录"),
UPDATE("update", "修改"),
ACKNOWLEDGE_OUTGOING("outGoing", "确认外送"),
CANCEL_DELIVERY("cancelDelivery", "取消外送"),
CHECK("check", "审核"),
CANCEL_CHECK("cancelCheck", "取消审核"),
SING_IN("singIn", "签收"),
CANCEL_SING_IN("cancelSingIn", "撤销签收"),
RECEIVE("receive", "接收"),
CANCEL_RECEIVE("cancelReceive", "撤销接收"),
MAKE("make", "制单"),
PRINT("print", "打印"),
;
private String code;
private String type;
OperationTypeEnum(String code, String type) {
this.code = code;
this.type = type;
}
public static OperationTypeEnum getType(String code) {
OperationTypeEnum result = null;
for (OperationTypeEnum s : values()) {
if (s.getCode().equals(code)) {
result = s;
break;
}
}
return result;
}
}
接口
@GetMapping("/getAllType")
public Response<OperationTypeEnum[]> getAllType() {
return Response.success(OperationTypeEnum.values());
}
响应结果
{
"code": 0,
"msg": "请求成功~",
"data": [
{
"code": "login",
"type": "登录"
},
{
"code": "update",
"type": "修改"
},
{
"code": "outGoing",
"type": "确认外送"
},
{
"code": "cancelDelivery",
"type": "取消外送"
},
{
"code": "check",
"type": "审核"
},
{
"code": "cancelCheck",
"type": "取消审核"
},
{
"code": "singIn",
"type": "签收"
},
{
"code": "cancelSingIn",
"type": "撤销签收"
},
{
"code": "receive",
"type": "接收"
},
{
"code": "cancelReceive",
"type": "撤销接收"
},
{
"code": "make",
"type": "制单"
},
{
"code": "print",
"type": "打印"
}
],
"success": true
}