场景说明
比如我们想打印返回参数的具体情况,如果里面有枚举类型,打印的参数不完整。
@Getter
@Setter
@ToString
public class ResultResponse {
private String name;
private Integer age;
private ResultEnum resultEnum;
}
public enum ResultEnum {
SUCCESS("0000", "成功"),
FAIL("0001", "失败"),
EXIST("0002", "已存在"),
SYS_ERROR("9999", "系统异常");
private String code;
private String msg;
ResultEnum(String code, String msg) {
this.code = code;
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
默认序列化方式
public class MainTest {
public static void main(String[] args) {
ResultResponse resultResponse = new ResultResponse();
resultResponse.setAge(11);
resultResponse.setName("张三");
resultResponse.setResultEnum(ResultEnum.SUCCESS);
System.out.println(JSONObject.toJSONString(resultResponse));
}
}
运行结果:{"age":11,"name":"张三","resultEnum":"SUCCESS"},并不是我想要的结果,枚举类型属性没打印出来
设置枚举序列化
public class MainTest {
public static void main(String[] args) {
ResultResponse resultResponse = new ResultResponse();
resultResponse.setAge(11);
resultResponse.setName("张三");
resultResponse.setResultEnum(ResultEnum.SUCCESS);
SerializeConfig config = new SerializeConfig();
config.configEnumAsJavaBean(ResultEnum.class);
System.out.println(JSONObject.toJSONString(resultResponse, config));
}
}
运行结果:{"age":11,"name":"张三","resultEnum":{"code":"0000","msg":"成功"}}