在进行项目迁移时,在原服务器中进行数据上传时没问题,迁移到新服务器时却报如下异常:
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.google.gson.JsonSyntaxException: 2019-07-02 10:30:25
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
root cause
com.google.gson.JsonSyntaxException: 2019-07-02 10:30:25
com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:81)
com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:66)
com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:41)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
com.google.gson.Gson.fromJson(Gson.java:810)
com.google.gson.Gson.fromJson(Gson.java:775)
com.google.gson.Gson.fromJson(Gson.java:724)
com.google.gson.Gson.fromJson(Gson.java:696)
实体类中对应属性为java.util.Date类型,项目中的原码为:
Gson gson=new Gson();
String reqJson=request.getParameter("json");
Modal bean=gson.fromJson(reqJson, Modal.class);
运行到gson解析JSON时报如上异常,代码没变,为什么在不同服务器上运行的结果就不一样了呢,后来各种搜索,才了解到Gson不同服务器在解析时间格式不一样,了解到这一点后,只需要在Gson实例化指定解析的格式即可,将
Gson gson=new Gson();替换为 Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
重新运行,终于通过了。