开发者社区> 问答> 正文

springmvc hibernate返回级联实体的json格式报错?报错


 
 
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"])


搜到很多类似的问题,但是没有看到一般的解决方案,返回级联对象的json格式咋这个费劲呢
大家怎么解决的呢

一方实体

@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;
	}
	
}


展开
收起
爱吃鱼的程序员 2020-06-14 18:14:33 725 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    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);


    多谢,,明白了为什么,在查怎么和springmvc继承和设置参数

    2020-06-14 18:14:52
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多