首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用JSON写文章时的奇怪ExecutionException

用JSON写文章时的奇怪ExecutionException
EN

Stack Overflow用户
提问于 2012-09-05 18:38:14
回答 2查看 949关注 0票数 3

我有这样的方法:

代码语言:javascript
复制
/**
 * 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);
}

我是这样测试的:

代码语言:javascript
复制
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);
    }

问题是它抛出了这个例外:

代码语言:javascript
复制
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");之后,它抛出了异常.

你们看到这个测试柱出什么问题了吗?

UPDATE:似乎这些问题来自试图将appointment呈现为JSON:

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-05 19:53:17

我能够通过深入研究错误跟踪来识别问题。基本上,我的Appointment对象有一些ManyToMany关系,在试图呈现这些关系时,renderJSON似乎给出了StackOverFlow

我已经创建了另一个Appointment对象,并且只将需要导出的字段复制到其中。

代码语言:javascript
复制
    Appointment appointmentOutput = new Appointment();
    appointmentOutput.id = appointment.id;
    appointmentOutput.name = appointment.name;

    renderJSON(appointmentOutput);
票数 0
EN

Stack Overflow用户

发布于 2012-11-06 14:13:17

这是因为:

Play框架与Google的JSON库Gson捆绑在一起,但是这个库的开发人员已经做出了一些设计选择,这使得它无法理想地生成将要发送到HTTP客户端的JSON。

Erik 推荐使用FlexSon

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12287741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档