Jak serializować java.util.List <E> do Json przy użyciu biblioteki Gson?

W moim serwletu mam następujący kod:

<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>

odpowiedź jest zwracana z ajax z tym kodem:

<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>

po uruchomieniu kodu nadal otrzymuję następujący błąd (z ajax):

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

Próbowałem także wyświetlić obiekt serializowanyList<Gps> w tej samej stronie java bez ajax, jak następuje:

<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>

Dostaję ten błąd:

<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>

Proszę pomóż!

EDYTOWAĆ: ostateczne rozwiązanie tego problemu

questionAnswers(2)

yourAnswerToTheQuestion