使用Gson

时间:2016-05-03 05:30:30

标签: json hibernate gson struts

我尝试了很多方法,我正在详细地发布我的问题。

这是我的父类

@Entity
@Table(name = "Project")
//@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class Project implements Serializable{
@Expose
int id;
@Expose
String projectName;
@Expose
String userID;
// Date dateCreated;
@Expose
String dateCreated;
@Expose
String status;
@Expose
String houseType;

@Expose
private List<UnitDetails> unitDetails = new ArrayList<>();
@Expose
private List<RoomDetails> roomDetails  = new ArrayList<>();
    @GeneratedValue
@Column(name = "id")
public int getId() {
    return id;
}
    @OneToMany( mappedBy="unitDetails", fetch = FetchType.LAZY)
public List<UnitDetails> getUnitDetails() {
    return unitDetails;
}

public void setUnitDetails(List<UnitDetails> unitDetails) {
    this.unitDetails = unitDetails;
}

@OneToMany(mappedBy="roomDetails", fetch = FetchType.LAZY)
public List<RoomDetails> getRoomDetails() {
    return roomDetails;
}

public void setRoomDetails(List<RoomDetails> roomDetails) {
    this.roomDetails = roomDetails;
}

这是我的子课程

@Entity
@Table(name="Unit_Details")
//@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class UnitDetails {
@Expose
int unitDetailsID;
@Expose
int designID;
@Expose
String unit;
@Expose
double length;
@Expose
double breadth;
@Expose
double height;
@Expose
String img;
@Expose
String type;
@Expose
String color;

@JsonIgnore
private Project unitDeTails;

@Id
@GeneratedValue
@Column(name="unitDetailsID", unique=true, nullable=false)
public int getUnitDetailsID() {
    return unitDetailsID;
}
    @ManyToOne  
@JoinColumn(name="id",  insertable=true, updatable=false)
public Project getUnitDetails() {
    return unitDeTails;
}

public void setUnitDetails(Project unitDetails) {
    this.unitDeTails = unitDetails;
}

这是我的控制器

public class ProjectController extends ActionSupport implements
    ModelDriven<Object> {
    public HttpHeaders index() {

    model = objProjectDAOImpl.getProjectDetails(userID);

    Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
    System.out.println(gson.toJson(model));
    model = gson.toJson(model);
    return new DefaultHttpHeaders("index").disableCaching();
}

我能够正确保存细节而不会出现任何循环错误。当我尝试检索时,我收到了循环引用错误。然后我使用Gson只显示必填字段,现在我得到下面的错误

HTTP Status 500 - A JSONObject text must begin with '{' at character 1 of 

我知道这不是JSON格式,但我认为GSON会照顾这个或让我知道我必须使用不同的方法来解决这个问题

它显示如下结果,看起来像一个数组 [{ “ID”:139, “项目名”: “ABCD”, “unitDetails”:[{ “unitDetailsID”:575,......

2 个答案:

答案 0 :(得分:0)

这可能不是正确的解决办法,但我不得不接受它。

在我设置父类后,我确保子类没有任何对父类的引用或者持有引用父类的任何对象,基本上将子引用设置为null,因此它不会#39进入循环。它对我有用

答案 1 :(得分:0)

为什么不使用@JsonIdentityInfo注释?我几个月前和使用

有同样的问题
@JsonIdentityInfo(property = "id", generator = ObjectIdGenerators.PropertyGenerator.class, scope = YourEntity.class)

做了这个伎俩。属性id对应于YourEntity中的getId()。 @JsonIdentityInfo注释来自杰克逊图书馆而不是Gson。作为替代方案,您可以使用同一个库中的@JsonManagedReference和/或@JsonBackReference

相关问题