-1

我正在尝试将从数据库收到的列表对象转换为 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。请有人可以解释为什么会这样。

这分别是我的 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;
}

时尚品牌类:

@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();
        }
    }
}
4

1 回答 1

1

这很可能是因为您在 Orders 类中引用了 FashionBrand,并且在 FashionBrand 类中引用了 Order。在生成 JSON 时,它们会不断地相互引用,从而导致 stackoverflow。我意识到 FashionBrand 的字段上有一个 @JsonIgnore 注释,但我仍然会说这个注释可能被忽略了。据我所知,这是一个 Jackson 注释,但您似乎使用的是 Google 的 GSON,它不会关心该注释。调试它应该很容易。

在您的 Orders 类中:

@Expose(serialize = false)
@ManyToOne
@JoinColumn(name = "fashionbrand")
private FashionBrand fashionBrand;

在您的 FashionBrand 课程中:

@JsonIgnore
@Transient
private List<Orders> orders = new ArrayList<>();
于 2018-07-24T16:03:15.910 回答