我正在尝试将从数据库接收的列表对象转换为Json,但是当我尝试进行转换时却得到了。
java.lang.StackOverflowError: null
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) ~[gson-2.8.0.jar:na]
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:976) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) ~[gson-2.8.0.jar:na]
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:976) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) ~[gson-2.8.0.jar:na]
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:976) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) ~[gson-2.8.0.jar:na]
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:976) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) ~[gson-2.8.0.jar:na]
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:976) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) ~[gson-2.8.0.jar:na]
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:976) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.0.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) ~[gson-2.8.0.jar:na]
at (and so on)
这是我的代码:
@RequestMapping(value = "/query/orders",method = RequestMethod.GET)
public String QueryOrderTable(HttpServletRequest request){
DataTableRequest<Orders> dataTableInRQ = new DataTableRequest<Orders>(request);
PaginationCriteria pagination = dataTableInRQ.getPaginationRequest();
String baseQuery = "SELECT id as id, time as time, amount as amount, status as status, style as style, fashionbrand as fashionbrand, user_id as user_id, (SELECT COUNT(1) FROM ORDERS) AS totalrecords FROM ORDERS";
String paginatedQuery = AppUtil.buildPaginatedQuery(baseQuery, pagination);
System.out.println(paginatedQuery);
Query query = entityManager.createNativeQuery(paginatedQuery, Orders.class);
@SuppressWarnings("unchecked")
List<Orders> ordersList = query.getResultList();
DataTableResults<Orders> dataTableResult = new DataTableResults<Orders>();
dataTableResult.setDraw(dataTableInRQ.getDraw());
dataTableResult.setListOfDataObjects(ordersList);
if (!AppUtil.isObjectEmpty(ordersList)) {
dataTableResult.setRecordsTotal(String.valueOf(ordersList.size())
);
if (dataTableInRQ.getPaginationRequest().isFilterByEmpty()) {
dataTableResult.setRecordsFiltered(String.valueOf(ordersList.size()));
} else {
dataTableResult.setRecordsFiltered(String.valueOf(ordersList.size()));
}
}
JSONSerializer serializer = new JSONSerializer();
return serializer.deepSerialize(dataTableResult);
}
这个想法是,我从数据库中检索了一个Orderobjects列表,将其传递给dataTableResult并将其发送到要由Jquerydatatable显示的前端,我得到了列表,但是当我尝试使用GSON()来尝试转换orederList或dataTableResult会引发错误。我切换到flexJson,这使我可以将orderList转换为json,但不能将dataTableResult转换为json。请有人可以解释为什么会这样。
这分别是我的Order类和dataTableResult类:
@Entity
public class Orders extends DefaultEntity {
@Expose(serialize = false)
@ManyToOne
@JoinColumn(name = "fashionbrand")
private FashionBrand fashionBrand;
private double amount;
@Enumerated(EnumType.STRING)
@Column(name = "style")
private Enum.ClothStyle clothStyle;
@Expose(serialize = false)
@OneToMany
private List<ClothModel> clothModels = new ArrayList<>();
@Enumerated(EnumType.STRING)
private Enum.Status status;
@Expose(serialize = false)
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
}
public class DataTableResults<T> {
/** The draw. */
private String draw;
/** The records filtered. */
private String recordsFiltered;
/** The records total. */
private String recordsTotal;
/** The list of data objects. */
@SerializedName("data")
List<T> listOfDataObjects;
}
这是ClothModel类:
@Entity
public class ClothModel extends DefaultEntity {
private int quantity;
private double amount;
@Transient
private String modelStructure;
private String savedUrl;
}
以及FashionBrand类:
@Entity
public class FashionBrand extends DefaultEntity {
private String brandName;
private String image;
private String website;
@Email
private String email;
private String password;
private String phoneNumber;
private String brandPhrase;
@JsonIgnore
@Transient
private List<Rating> ratings = new ArrayList<>();
private String location;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "pricing_id")
private Pricing pricing;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "services_id")
private Services services;
@JsonIgnore
@Transient
private List<Orders> orders = new ArrayList<>();
@JsonIgnore
@Transient
private List<Employee> employees = new ArrayList<>();
}
最后是默认实体类:
@MappedSuperclass
public class DefaultEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(columnDefinition = "serial")
private long id;
@Column(name = "Time",columnDefinition= "TIMESTAMP WITH TIME ZONE")
@Temporal(TemporalType.TIMESTAMP)
private Date dateCreated;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Date getDateCreated() {
return dateCreated;
}
public void setDateCreated() {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
dateCreated = dateFormat.parse(dateFormat.format(new Date()));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
这很可能是因为您在Orders类中具有对FashionBrand的引用,而在FashionBrand类中具有对Order的引用。在生成JSON时,它们会相互引用,从而导致stackoverflow。我意识到FashionBrand中的字段上有一个@JsonIgnore批注,但我仍然会说可能忽略了此批注。据我所知,这是一个杰克逊注解,但您似乎正在使用Google的GSON,它不会关心该注解。调试起来应该很容易。
在您的Orders类中:
@Expose(serialize = false)
@ManyToOne
@JoinColumn(name = "fashionbrand")
private FashionBrand fashionBrand;
在您的FashionBrand课程中:
@JsonIgnore
@Transient
private List<Orders> orders = new ArrayList<>();