Gson.toJson Stackoverflow错误

时间:2016-02-22 10:30:51

标签: java json playframework gson stack-overflow

我在java中使用play框架(ebean是我的ORM),我有这个类:

@Entity
@Table(name = "reservation")
public class Reservation extends Model implements Validation {
    @Id
    @Column(name = "reservationId", columnDefinition = "BIGINT")
    @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonIgnore
    private long reservationId;
    @Column(name = "restaurantId", columnDefinition = "BIGINT")
    private long restaurantId;
    @Column(name = "dateTime" , columnDefinition = "DATETIME")
    @JsonFormat(pattern = "dd/mm/yyyy HH:mm:ss")
    @Formats.DateTime( pattern = "dd/mm/yyyy HH:mm:ss")
    private Timestamp dateTime;
    @Column(name = "guestCount")
    private int guestCount;
    @Column(length = 300)
    private String note;

    public static Reservation.Finder<String, Reservation> find = new Model.Finder<String, Reservation>(String.class, Reservation.class);

我正在尝试使用Gson库序列化它。我试着这样做

return new Gson().toJson(Reservation.find.where().eq("reservationId", id).findUnique(););

但我总是得到这个错误:

akka.actor.ActorSystemImpl - Uncaught fatal error from thread [application-akka.actor.default-dispatcher-2] shutting down ActorSystem [application]
java.lang.StackOverflowError: null
    at java.text.DateFormatSymbols.initializeData(DateFormatSymbols.java:684) ~[na:1.8.0_71]
    at java.text.DateFormatSymbols.<init>(DateFormatSymbols.java:146) ~[na:1.8.0_71]
    at sun.util.locale.provider.DateFormatSymbolsProviderImpl.getInstance(DateFormatSymbolsProviderImpl.java:85) ~[na:1.8.0_71]
    at java.text.DateFormatSymbols.getProviderInstance(DateFormatSymbols.java:359) ~[na:1.8.0_71]
    at java.text.DateFormatSymbols.getInstance(DateFormatSymbols.java:335) ~[na:1.8.0_71]
    at java.util.Calendar.getDisplayName(Calendar.java:2110) ~[na:1.8.0_71]
    at java.text.SimpleDateFormat.subFormat(SimpleDateFormat.java:1125) ~[na:1.8.0_71]
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:966) ~[na:1.8.0_71]
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:936) ~[na:1.8.0_71]
    at java.text.DateFormat.format(DateFormat.java:345) ~[na:1.8.0_71]
    at com.google.gson.internal.bind.DateTypeAdapter.write(DateTypeAdapter.java:83) ~[gson-2.5.jar:na]
    at com.google.gson.internal.bind.DateTypeAdapter.write(DateTypeAdapter.java:41) ~[gson-2.5.jar:na]
    at com.google.gson.internal.bind.TypeAdapters$26$1.write(TypeAdapters.java:607) ~[gson-2.5.jar:na]
    at com.google.gson.internal.bind.TypeAdapters$26$1.write(TypeAdapters.java:600) ~[gson-2.5.jar:na]

我正在阅读很多相关内容,我理解在序列化Reservation对象时可能会出现无限循环,但由于该对象与任何其他类没有任何关系,我不明白为什么它发生的情况。

1 个答案:

答案 0 :(得分:2)

问题在于Gson。 Gson已经知道将ebean转换为Json的问题。我用杰克逊取而代之,现在就可以了。