Java 使用JSON进行POST时出现奇怪的ExecutionException
我有这个方法:Java 使用JSON进行POST时出现奇怪的ExecutionException,java,playframework,Java,Playframework,我有这个方法: /** * 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);
/**
* 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);
}
我也不知道为什么
它打印出Logger.info(“服务刷新”)代码>之后将引发异常
你们觉得这个测试柱有什么问题吗
更新:问题似乎来自尝试将约会
呈现为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
对象具有一些manytomy
关系,并且在尝试渲染这些关系时,renderJSON似乎给出了StackOverFlow
我已经创建了另一个Appointment
对象,并仅将需要导出的字段复制到其中
Appointment appointmentOutput = new Appointment();
appointmentOutput.id = appointment.id;
appointmentOutput.name = appointment.name;
renderJSON(appointmentOutput);
这是因为:
Play框架与Google的JSON库Gson捆绑在一起,但是
该库的开发人员做出了一些设计选择,这些选择保持了
它并不适合生成要发送到的JSON
HTTP客户端
Erik Bakker使用我在游戏框架方面没有经验,但我希望能帮助你。谢谢,但没有帮助。。。
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 appointmentOutput = new Appointment();
appointmentOutput.id = appointment.id;
appointmentOutput.name = appointment.name;
renderJSON(appointmentOutput);