Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.springframework.validation.support.BindingAwareModelMap["course"]->com.thon.entity.course.Course["createBy"]->com.thon.entity.system.User_$$_jvstdcb_0["handler"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.springframework.validation.support.BindingAwareModelMap["course"]->com.thon.entity.course.Course["createBy"]->com.thon.entity.system.User_$$_jvstdcb_0["handler"])
一方实体
@JsonIgnoreProperties(ignoreUnknown=true) public class Course extends IdEntity { private static final long serialVersionUID = 2118439396249691137L; private String name; private User createBy; private List<CourseTag> courseTags; public String getName() { return name; } public void setName(String name) { this.name = name; } @OneToMany(fetch = FetchType.LAZY, mappedBy = "course",cascade = CascadeType.ALL) public List<CourseTag> getCourseTags() { return courseTags; } public void setCourseTags(List<CourseTag> courseTags) { this.courseTags = courseTags; } @ManyToOne(fetch = FetchType.LAZY) public User getCreateBy() { return createBy; } public void setCreateBy(User createBy) { this.createBy = createBy; } }
@JsonIgnoreProperties(ignoreUnknown=true) public class CourseTag extends IdEntity { private static final long serialVersionUID = 7790694935170125722L; private Integer region; private Integer tagId; private Course course; public Integer getRegion() { return region; } public void setRegion(Integer region) { this.region = region; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "courseId") public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } public Integer getTagId() { return tagId; } public void setTagId(Integer tagId) { this.tagId = tagId; } }
coursetag.setcourse(course)设置了吗?那就好顶一下下
在使用jackson的时候,需要根据情况设置几个参数的,例如输出的属性风格、允许单引号、允许不带引号的字段名称等等。
使用objectMapper.configure来设置各种参数,例如:
输出属性风格:<spanstyle="color:#E53333;">属性为空("")或者为NULL时都不序列化 <spanstyle="color:#E53333;">
objectMapper.setSerializationInclusion(Include.NON_EMPTY);
允许单引号、允许不带引号的字段名称:
objectMapper.configure(Feature.ALLOW_SINGLE_QUOTES,true);
objectMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES,true);
Enum的序列化与反序列化:
objectMapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。