java 枚举自定义多个属性,获取对象出现的问题 @JsonFormat(shape = JsonFormat.Shape.OBJECT)
枚举类遇到的问题
·
#1.首先是问题的出现场景,由于我进行获取枚举对象,返回前端内容时遇到对象值没有
前端获取的值
但是枚举对象中是有值的
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum MemberLevelEnum {
VIP_NORMAL(1,"vip_normal",1,new BigDecimal("59.9"),new BigDecimal("29.9"),new BigDecimal("29.9"),"单月购买"),
VIP_NORMAL_CONTINUE(2,"vip_normal_continue",1,new BigDecimal("59.9"),new BigDecimal("29.9"),new BigDecimal("12"),"连续包月"),
;
/**
* 会员等级
*/
private Integer memberLevel;
/**
* 会员等级code
*/
private String memberLevelCode;
/**
* 会员有效期,单位为月
*/
private Integer month;
/**
* 原价
*/
private BigDecimal oldPrice;
/**
* 现价
*/
private BigDecimal nowPrice;
/**
* 自动续费价格
*/
private BigDecimal continuePrice;
/**
* 描述
*/
private String desc;
private MemberLevelEnum(Integer memberLevel, String memberLevelCode, Integer month, BigDecimal oldPrice,
BigDecimal nowPrice, BigDecimal continuePrice, String desc) {
this.memberLevel = memberLevel;
this.memberLevelCode = memberLevelCode;
this.month = month;
this.oldPrice = oldPrice;
this.nowPrice = nowPrice;
this.continuePrice = continuePrice;
this.desc = desc;
}
public Integer getMemberLevel() {
return memberLevel;
}
public void setMemberLevel(Integer memberLevel) {
this.memberLevel = memberLevel;
}
public String getMemberLevelCode() {
return memberLevelCode;
}
public void setMemberLevelCode(String memberLevelCode) {
this.memberLevelCode = memberLevelCode;
}
public Integer getMonth() {
return month;
}
public void setMonth(Integer month) {
this.month = month;
}
public BigDecimal getOldPrice() {
return oldPrice;
}
public void setOldPrice(BigDecimal oldPrice) {
this.oldPrice = oldPrice;
}
public BigDecimal getNowPrice() {
return nowPrice;
}
public void setNowPrice(BigDecimal nowPrice) {
this.nowPrice = nowPrice;
}
public BigDecimal getContinuePrice() {
return continuePrice;
}
public void setContinuePrice(BigDecimal continuePrice) {
this.continuePrice = continuePrice;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public static MemberLevelEnum getByCode(String memberLevelCode) {
return Arrays.stream(MemberLevelEnum.values()).filter(item -> StringUtils.equals(memberLevelCode, item.memberLevelCode)).findFirst().orElse(null);
}
}
#2.原因是没有进行格式化导致返回前端枚举对象没有值,直接进行获取到的值进行格式化,依然不起作用。
#3解决问题
在网上查找资料,通过在枚举类加上注解@JsonFormat(shape = JsonFormat.Shape.OBJECT)
解决了枚举类返回给前端格式化的问题
Shape.OBJECT 表示序列化的一种格式或者类型,可以有多种类型。
底层源码上说明会有多种序列化类型,根据自己需要选择一种即可!
最后再去通过枚举类去获取自定义对象前端就会有值了,问题就解决了。
更多推荐
已为社区贡献1条内容
所有评论(0)