Wie serialisiere ich eine java.util.List <E> nach Json mit der Gson Library?

In meinem Servlet habe ich folgenden Code:

<code>    response.setContentType("application/json");//set json content type
    PrintWriter out = response.getWriter();

   EntityManagerFactory emf=Persistence.createEntityManagerFactory("webPU");
   GpsJpaController gjc=new GpsJpaController(emf);    
   java.util.List<Gps> listGps=gjc.findGpsEntities();//retrieve the list of GPS from DB 

   Gson gson=new Gson();
   String json=gson.toJson(listGps); //convert List<Gps> to json
   out.println(json);
</code>

Antwort wird von Ajax mit diesem Code zurückgegeben:

<code>$.ajax({ 
           type: "GET", 
           url: "getInfoGeoActive",
           dataType: 'json',
           error: function(xhr, ajaxOptions, thrownError){ $("#content").empty().append("* Error from server cause: "+thrownError).addClass("error"); } ,
           success:function(data){$("#content").empty().append(data);}
             });
</code>

Wenn ich den Code ausführe, erhalte ich immer noch die folgende Fehlermeldung (von Ajax):

<code>  Error from server cause:Internal Servlet Error
</code>

Ich habe auch versucht, das serialisierte Objekt anzuzeigenList<Gps> auf derselben Java-Seite ohne Ajax wie folgt:

<code>EntityManagerFactory emf=Persistence.createEntityManagerFactory("JavaApplication21PU");
        GpsJpaController gjc=new GpsJpaController(emf);
        java.util.List<Gps> listGps=gjc.findGpsEntities();

        Gson gson=new Gson();
        String json=gson.toJson(listGps);

        System.out.println(json);
</code>

Ich erhalte diesen Fehler:

<code>Exception in thread "main" java.lang.StackOverflowError
    at java.lang.StrictMath.floorOrCeil(StrictMath.java:355)
    at java.lang.StrictMath.floor(StrictMath.java:340)
    at java.lang.Math.floor(Math.java:424)
    at sun.misc.FloatingDecimal.dtoa(FloatingDecimal.java:620)
    at sun.misc.FloatingDecimal.<init>(FloatingDecimal.java:459)
    at java.lang.Double.toString(Double.java:196)
    at java.lang.Double.toString(Double.java:633)
    at com.google.gson.stream.JsonWriter.value(JsonWriter.java:441)
    at com.google.gson.Gson$4.write(Gson.java:270)
    at com.google.gson.Gson$4.write(Gson.java:255)
</code>

Bitte um Hilfe!

BEARBEITEN: die endgültige Lösung dieses Problems

Antworten auf die Frage(2)

Ihre Antwort auf die Frage