2012-09-05 2 views
3

J'ai cette méthode:ExecutionException étrange lorsque vous faites POST avec JSON

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

Je teste comme ceci:

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

Eh bien, la question est qu'il est de lancer cette 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) 

et je n'arrive pas à trouver pourquoi.

Il imprime Logger.info("services refreshed"); après qu'il jette l'exception ...

-vous les gars voyez un problème avec ce POST test?

MISE À JOUR: Il semble que les problèmes vient d'essayer de rendre la appointment comme 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) 
+0

Je n'ai pas d'expérience dans le cadre de lecture, mais je l'espère http://stackoverflow.com/questions/9510762/proper-way-of-writing-functionaltest-in- playframework vous aidera. –

+0

Merci mais ça n'a pas aidé ... –

Répondre

0

j'ai pu identifier les problèmes en allant vers le bas et l'étude de la trace d'erreur. Fondamentalement mon objet Appointment a quelques relations ManyToMany et il semble que renderJSON donnait StackOverFlow en essayant de rendre ces relations.

J'ai créé un autre objet Appointment et n'y copie que les champs que j'ai besoin d'exporter.

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

    renderJSON(appointmentOutput); 
1

Il est parce que:

Le cadre de lecture est livré avec une bibliothèque de Google JSON Gson, mais les développeurs de cette bibliothèque ont fait des choix de conception qui maintiennent il d'être idéal pour générer JSON cela va être envoyé au le client HTTP.

Erik Bakker recommended utiliser FlexSon

Questions connexes