disable SerializationFeature.FAIL_ON_EMPTY_BEANS

masterkgw 发布于 2017/01/20 14:23
阅读 4K+
收藏 0

在用springmvc返回json格式的数据时出现这个问题。

项目架构:struts2+springmvc+hibernate ,(一个老项目需升级,要求用springmvc,为了方便就和struts2一起用了

控制层方法:

@RequestMapping(value = "user-dept/{uid}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> deptList(@PathVariable("uid") String uid) {

        this.map = new HashMap<String, Object>();
        //根据用户查询部门
        List<AlertDepartment>   depts = this.getBaseAllService().getUserService().deptByUid(uid);
        map.put("depts", depts);
        return  map;
    }



实体类:
package fau.alert.entity;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import fau.alert.util.CustomMapper;
import org.codehaus.jackson.annotate.*;
import org.hibernate.Hibernate;
import org.hibernate.annotations.GenericGenerator;
import org.json.simple.JSONArray;
import org.json.simple.JSONAware;
import org.json.simple.JSONObject;

/**
 * @desc 单位表
 * @author liy
 * @datetime 2015年5月9日,下午5:28:25
 */
@Entity
@Table(name = "ALERT_DEPARTMENT")
public class AlertDepartment implements Serializable,JSONAware {

	private static final long serialVersionUID = 1391376963202570171L;
	
	public AlertDepartment(){
		
	}
	
	public AlertDepartment(String id){
		this.id = id;
	}
	
	private String id;
	private String name;
	private String iskey;
	private String levelType;
	private String sortKey;
	
	private Set<AlertUserRole> alertUserRoles;
	private Set<AlertRole> roleSet;
	private AlertDepartment alertDepartment;
	private Set<AlertDepartment> deptSet;
	private Set<AlertBuildings> buildings;


    @Id
	@GenericGenerator(name = "generator", strategy = "uuid.hex")
	@GeneratedValue(generator = "generator")
	@Column(name = "ID", length = 32)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(name = "NAME", length = 50)
	public String getName() {
		return name;
	}

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

	@Column(name = "ISKEY", length = 1)
	public String getIskey() {
		return iskey;
	}

	public void setIskey(String iskey) {
		this.iskey = iskey;
	}

	@Column(name = "LEVEL_TYPE", length = 1)
	public String getLevelType() {
		return levelType;
	}

	public void setLevelType(String levelType) {
		this.levelType = levelType;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "alertDepartment")
    @JsonIgnore
	public Set<AlertUserRole> getAlertUserRoles() {
		return alertUserRoles;
	}

	public void setAlertUserRoles(Set<AlertUserRole> alertUserRoles) {
		this.alertUserRoles = alertUserRoles;
	}

    @JsonIgnore
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "ALERT_DEPT_ROLE", joinColumns = {@JoinColumn(name = "DEPT_ID")}, inverseJoinColumns = {@JoinColumn(name = "ROLE_ID")})
	public Set<AlertRole> getRoleSet() {
		return roleSet;
	}

	public void setRoleSet(Set<AlertRole> roleSet) {
		this.roleSet = roleSet;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "PID")
	public AlertDepartment getAlertDepartment() {
		return alertDepartment;
	}

	public void setAlertDepartment(AlertDepartment alertDepartment) {
		this.alertDepartment = alertDepartment;
	}

    @JsonIgnore
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "alertDepartment")
    public Set<AlertDepartment> getDeptSet() {
        return deptSet;
    }

	public void setDeptSet(Set<AlertDepartment> deptSet) {
		this.deptSet = deptSet;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "ALERT_BUILD_DEPT", joinColumns = {@JoinColumn(name = "DID")}, inverseJoinColumns = {@JoinColumn(name = "BID")})
    @JsonIgnore
	public Set<AlertBuildings> getBuildings() {
		return buildings;
	}

	public void setBuildings(Set<AlertBuildings> buildings) {
		this.buildings = buildings;
	}
	
	@Column(name = "ORDER_KEY", length = 9)
	public String getSortKey() {
		return sortKey;
	}

	public void setSortKey(String sortKey) {
		this.sortKey = sortKey;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String toJSONString() {
		JSONObject obj = new JSONObject();
		obj.put("id", id);
		obj.put("text", name);
		if(Hibernate.isInitialized(deptSet)){
			if(0 != deptSet.size()){
				JSONArray  array = new JSONArray();
				array.addAll(deptSet);
				obj.put("children",array);
				obj.put("state", "closed");
			}
		}
		return obj.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AlertDepartment other = (AlertDepartment) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
}



前台:
//后台查询用户部门
            $.get("mvc/system/user-dept/"+userId+"?"+Math.random(),function(resp){
                var depts = resp.depts;
                //拼接下拉框
                var options = $("#user_dept");
                $.each(depts, function() {
                    options.append($("<option />").val(depts.id).text(depts.Name));
                });
            },"json");



错误:当contrller执行完成后给前台返数据时,页面出现这个问题:



试了很多方法都不起作用,望遇到的朋友指点一二,多谢。



加载中
0
风翔飞
风翔飞
你确定hibernate返回数据了吗?
delta-2017
delta-2017
回复 @masterkgw : 可是我定义的实体类都是普通的Java类型字段,比如String,Date。 为什么还是会报这个错误?
masterkgw
masterkgw
问题已经解决,问题出在在实体类上,需要把我这个实体类中所有的set映射以及与这个实体类关联的实体类里的set映射都用@JsonIgnore注解,意思是在向前台解析的时候不解析被@JsonIgnore注解的对象。
返回顶部
顶部