iyuks
2021-06-10 11:56
采纳率: 95.7%
浏览 16

jackson序列化怎么报错了?什么原因?

实体类
   public class Employer {
    /**
     * 忽略序列化
     */
    @JsonIgnore
    private String name;
    private boolean flag;
    /**
     * 序列化名字
     */
    @JsonProperty("AGE")
    private int age;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Object data;
    /**
     * 序列化格式
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date date;


    public String getName() {
        return name;
    }

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

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return "Employer{" +
                "name='" + name + '\'' +
                ", flag=" + flag +
                ", age=" + age +
                ", data=" + data +
                ", date=" + date +
                '}';
    }
}




main方法里的代码 
       Employer employer = new Employer();
        employer.setName("jack");
        employer.setAge(19);
        employer.setFlag(true);
        employer.setData(new Object());
        employer.setDate(new Date());

        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(employer);
        System.out.println(s);
  • 好问题 提建议
  • 收藏

2条回答 默认 最新

  • iyuks 2021-09-16 10:23
    已采纳

    不知道原因

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • li.siyuan 2021-06-10 12:18

    没这么做过,序列化的话,我是这么做的...

    public class User implements Serializable {
        private static final long serialVersionUID = 1L;
    }
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题