用JSON做POST怪时为ExecutionException(Strange ExecutionE

2019-09-22 04:16发布

我有这样的方法:

/**
 * POST
 */
public static void create(String body) {
    Logger.info("APPOINTMENT create - json string: %s", body);

    Appointment appointment = null;
    try {
        appointment = new Gson().fromJson(body, Appointment.class);

        //services are detached, re-take them from db
        List<Service> refreshedServices = new ArrayList<Service>();
        for (final Service ser : appointment.services) {
            Service service = Service.findById(ser.id);
            refreshedServices.add(service);
        }
        appointment.services = refreshedServices;
        appointment.save();
        appointment.refresh();
        Logger.info("services refreshed");
    } catch (Exception ex) {
        Logger
                .info(
                        "An error has occured when trying to create an APPOINTMENT %s",
                        ex);
        response.status = Http.StatusCode.INTERNAL_ERROR;
        renderJSON(new StatusMessage(
                "An internal error has occured. Please try again later."));
    }

    renderJSON(appointment);
}

我喜欢这个测试吧:

public void createAppointment(final Contact contact, final List<Service> services) throws Exception {
        Appointment app = new Appointment();
        app.userUid = "cristi-uid";
        app.name = "app1";
        app.contact = contact;

        String serviceAsJson = "{\"userUid\":\"cristi-uid\",\"name\":\"app1\",\"allDay\":false,\"contact\":{\"id\":"+contact.id+"},\"services\":[{\"id\":\""+services.get(0).getId()+"\"},{\"id\":\""+services.get(1).getId()+"\"}]}";
        Response response = POST("/appointment/create", "application/json", serviceAsJson);

        Logger.info("POST was done");
        Appointment appReturned = new Gson().fromJson(response.out.toString(), Appointment.class);

        Logger.info("appointment create response: %s", response.out.toString());

        assertIsOk(response);
        assertEquals(appReturned.name, app.name);
    }

那么,问题是它抛出这个异常:

java.lang.RuntimeException: java.util.concurrent.ExecutionException: play.exceptions.JavaExecutionException
at play.test.FunctionalTest.makeRequest(FunctionalTest.java:304)
at play.test.FunctionalTest.makeRequest(FunctionalTest.java:310)
at play.test.FunctionalTest.POST(FunctionalTest.java:152)
at play.test.FunctionalTest.POST(FunctionalTest.java:120)
at play.test.FunctionalTest.POST(FunctionalTest.java:116)
at AppointmentsTest.createAppointment(AppointmentsTest.java:61)

和我无法找出原因。

它打印出Logger.info("services refreshed"); 之后,它被抛出异常...

难道你们看到什么毛病这个测试POST?

更新 :看来,问题来源于试图渲染appointment为JSON:

Caused by: java.lang.StackOverflowError
at java.util.LinkedHashMap$LinkedHashIterator.(LinkedHashMap.java:345)
at java.util.LinkedHashMap$LinkedHashIterator.(LinkedHashMap.java:345)
at java.util.LinkedHashMap$ValueIterator.(LinkedHashMap.java:387)
at java.util.LinkedHashMap$ValueIterator.(LinkedHashMap.java:387)
at java.util.LinkedHashMap.newValueIterator(LinkedHashMap.java:397)
at java.util.HashMap$Values.iterator(HashMap.java:910)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:192)
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:879)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)

Answer 1:

这是因为:

播放框架捆绑了谷歌的JSON库GSON,但这个库的开发已经取得了一些设计选择,保持它是理想的,用于产生将被发送到HTTP客户端JSON。

埃里克·巴克推荐使用FlexSon



Answer 2:

我能够下降和研究错误跟踪,以确定问题。 基本上我的Appointment对象具有一定的ManyToMany关系,似乎renderJSON是给StackOverFlow努力使这些关系的时候。

我创建了另一个Appointment对象,并复制到它只是我需要导出的字段。

    Appointment appointmentOutput = new Appointment();
    appointmentOutput.id = appointment.id;
    appointmentOutput.name = appointment.name;

    renderJSON(appointmentOutput);


文章来源: Strange ExecutionException when doing POST with JSON