当前位置: 首页 > 知识库问答 >
问题:

当试图将类转换为Json时,我遇到了stackoverflow错误[重复]

乐钱青
2023-03-14

我正在尝试将从数据库接收到的列表对象转换为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();
        }
    }
}

共有1个答案

卢健
2023-03-14

这很可能是因为您在Orders类中引用了FashionBrand,而在FashionBrand类中引用的是Order。在生成JSON时,它们不断地相互引用,从而产生堆栈流。我意识到FashionBrand中的字段上有一个@JsonIgnore注释,但我还是想说这个注释可能被忽略了。据我所知,这是一个杰克逊注释,但你似乎在使用谷歌的GSON,它不会在意这个注释。这应该很容易调试。

在您的订单类中:

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

在您的时尚品牌类中:

@JsonIgnore
@Transient
private List<Orders> orders = new ArrayList<>();
 类似资料:
  • 我正在尝试从地图转换键iterable,但当我这样做时,我得到错误: 统计数字produceTypeData。钥匙不是一种功能 我将遵循这个问题的答案:如何将映射键转换为数组?让它工作。 当我尝试另一种方法(使用)时,我得到一个不同的错误,即: 类型IterableIterator不是数组类型。 在评论中我读到,在本例中,应该将数组括起来。扩展语法中的from()语句,但当我这样做时,我也会得到错

  • 我试图将一个JSON有效负载转换为Avro以发布到一个Kafka主题。但是,当我执行Dataweave转换时,我会得到一个“no type”错误。我不确定是什么导致了这个错误。我最初认为这可能是由于转换不知道入站有效负载上的MIME类型。因此,我已经确保将其设置为,但这并没有什么不同。 Avro模式

  • My AccountEntityToAccount方法出现错误,返回类型Transaction是抽象类或接口。提供非抽象/非接口结果类型或工厂方法。AccountEntityToAccount(AccountEntity AccountEntity); 我怎样才能解决这个问题。我的事务类应该是抽象的,我无法更改抽象关键字

  • 我需要将一个远程图像转换成bas64给定其URL,但我运行在CORS错误,不知道如何工作。 在这个问题上,我遵循了一些解决方案:如何使用javascript将图像转换为base64字符串 我的示例图是:https://blog.xenproject.org/wp-content/uploads/2014/10/Testing.jpg 方法1(FileReader): 这将产生以下错误: XMLHt

  • 在我的项目中,我使用了带有okta验证的reactjs程序。当我运行reactjs程序时,输出变得很好。 但是,当我使用capacitorjs将reactjs网页转换为移动应用程序时,该网页不会显示在手机中 我已经按照以下url中的步骤将网页转换为移动应用程序 https://medium.com/how-to-react/convert-your-existing-react-js-app-to