使用JSON执行POST时出现奇怪的ExecutionException [英] Strange ExecutionException when doing POST with JSON

查看:128
本文介绍了使用JSON执行POST时出现奇怪的ExecutionException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种方法:

/**
 * 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);
    }

好吧,问题在于它引发了此异常:

Well, the issue is that it is throwing this exception:

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"); ...

It prints out Logger.info("services refreshed"); after that it is throwing the exception...

你们看到这个测试POST有什么问题吗?

Do you guys see anything wrong with this test POST?

更新:似乎问题出在尝试将appointment呈现为JSON:

UPDATE: Seems that the issues comes from trying to render the appointment as 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)

推荐答案

我能够通过仔细研究错误跟踪来识别问题.基本上,我的Appointment对象具有一些ManyToMany关系,并且在尝试呈现这些关系时,renderJSON似乎提供了StackOverFlow.

I was able to identify the issues by going down and studying the error trace. Basically my Appointment object has some ManyToMany relations and it seems that renderJSON was giving StackOverFlow when trying to render those relations.

我创建了另一个Appointment对象,并将仅需要导出的字段复制到该对象中.

I've created another Appointment object and copy into it only the fields I need to be exported.

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

    renderJSON(appointmentOutput);

这篇关于使用JSON执行POST时出现奇怪的ExecutionException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆