java.lang.IllegalArgumentException:类java.text.DecimalFormat声明了多个名为maximumIntegerDigits的JSON字段

时间:2016-03-20 13:38:10

标签: java gson illegalargumentexception decimalformat

我正在对servlet进行ajax调用。我希望数据从servlet返回到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);
}

我在类路径中添加了GSon.jar。但是我得到了一些错误:完整列表如下:我在这里缺少什么?

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)

3 个答案:

答案 0 :(得分:5)

我的情况是导致错误的DO中的SimpleDateFormat字段,添加后 transient,问题解决了

答案 1 :(得分:3)

您实际上不太可能希望将类型为MVC Application的字段序列化为JSON。如果您可以修改DecimalFormat类,则可以考虑删除该字段,或将其设为KitapData,这将告诉Gson(和其他序列化程序)忽略该字段。

transient

如果您无法修改private transient DecimalFormat whateverTheFieldIsCalled; 课程,则还有其他方法可以告诉Gson从序列化中排除该字段 - 例如,请参阅Gson: How to exclude specific fields from Serialization without annotations

答案 2 :(得分:0)

您的班级KitapDataDecimalFormat个属性。 Gson正在贬低它,因为它不是标准的Java类型,它内置了支持,例如StringNumberBooleanDateList,等。

您需要删除该DecimalFormat属性。不仅因为Gson不能在没有registered a type adapter的情况下自动消化它,而且因为它不可能代表“bean属性”而且它本质上是紧耦合的。此外,NumberFormat就像DateFormat所有朋友而不是线程安全。将它作为bean属性表明它可以在多个线程之间共享。这可能会导致格式化或甚至异常。

也许您只想存储格式模式或其他相关属性。在这种情况下,请将它们存储为StringInteger等,并确保代码始终在方法本地范围内基于这些“松散”属性创建全新的DecimalFormat实例。