6

I am making an ajax call to the servlet. I want the data back from the servlet to the jsp:

PrintWriter out = response.getWriter();
    String isbn = (String) request.getParameter("isbn");
    BookDetail bd = new BookDetail();
    if(bd.ISBNFound(isbn)){
    ArrayList<KitapData> Books = bd.LoadBooksByISBN(isbn);

    Gson gson = new Gson();
    String json = gson.toJson(Books);
    response.setContentType("application/json");
    System.out.print(json);      
    out.print(json);
}

I've added the GSon.jar in the classpath. But Iam getting some error: Complete list is as follows: What am i missing here?

WARNING:   StandardWrapperValve[Book_CheckISBN]: Servlet.service() for servlet Book_CheckISBN threw exception
java.lang.IllegalArgumentException: class java.text.DecimalFormat declares multiple JSON fields named maximumIntegerDigits
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:122)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
    at com.google.gson.Gson.getAdapter(Gson.java:356)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
    at com.google.gson.Gson.getAdapter(Gson.java:356)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:55)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
    at com.google.gson.Gson.toJson(Gson.java:593)
    at com.google.gson.Gson.toJson(Gson.java:572)
    at com.google.gson.Gson.toJson(Gson.java:527)
    at com.google.gson.Gson.toJson(Gson.java:507)
    at book.ctrl.Book_CheckISBN.doPost(Book_CheckISBN.java:41)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:722)
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Pranjal Choladhara
  • 845
  • 2
  • 14
  • 35

3 Answers3

12

My case is a SimpleDateFormat field in the DO causing the error,
after adding transient to it, problem solved

Maxwell Cheng
  • 1,050
  • 10
  • 17
  • @JavierHeisecke, its a keyword to skip certain variable during serialization, in my case its just no need to save a SimpleDateFormat field in the DO and pass it around, so we can skip it by adding transient. – Maxwell Cheng May 28 '19 at 10:37
3

It is unlikely that you actually want to be serializing a field of type DecimalFormat to JSON. If you can modify the KitapData class, you can consider removing the field, or making it transient which will tell Gson (and other serializers) to ignore the field.

private transient DecimalFormat whateverTheFieldIsCalled;

If you can't modify the KitapData class there are other ways to tell Gson to exclude the field from serialization - for example see Gson: How to exclude specific fields from Serialization without annotations

Community
  • 1
  • 1
CupawnTae
  • 14,192
  • 3
  • 29
  • 60
0

Your class KitapData has somewhere a DecimalFormat property. Gson is falling over it because it's not a standard Java type for which it has builtin support such as String, Number, Boolean, Date, List, etc.

You need to remove that DecimalFormat property. Not only because Gson can't digest it automatically without having registered a type adapter, but also because it can impossibly represent a "bean property" and it's essentially tight-coupling. Moreover, NumberFormat is like DateFormat all friends not threadsafe. Having it as a bean property suggests that it's shareable among multiple threads. This may result in corrupt formatting or even exceptions.

Perhaps you only wanted to store a the format pattern or other related properties. In that case, store them as String, Integer, etc instead and make sure the code always creates a brand new DecimalFormat instance in the method local scope based on those "loose" properties.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555