【发布时间】:2016-05-03 05:30:30
【问题描述】:
我尝试了很多方法,我正在详细发布我的问题。
这是我的父类
@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,"projectName":"ABCD","unitDetails":[{"unitDetailsID":575,......
【问题讨论】:
-
一个丑陋的修复将添加 { 在输出的开头和 } 在输出的末尾
-
如果是这种情况,我将使用下面的内容,它肯定会发送一个 JSON,但我想知道我在这里缺少什么 GSON String json = gson.toJson(listProject);模型 = json.substring(1, json.length()-1);
-
如果我只是尝试返回如下模型,那么我会得到层次结构问题 public HttpHeaders index() { model = objProjectDAOImpl.getProjectDetails(userID); return new DefaultHttpHeaders("index").disableCaching(); } net.sf.json.JSONException: 层次结构中有一个循环!
-
Struts ModelDriven的可能重复
-
我已经详细说明了这个问题的所有内容。所以如果有任何帮助,另一个与它相关但不一样
标签: json hibernate gson struts