当前位置:   article > 正文

Gson解析时间时报com.google.gson.JsonSyntaxException异常问题的解决

com.google.gson.jsonsyntaxexception

Gson解析时间时报com.google.gson.JsonSyntaxException异常问题的解决

示例代码

  @Test
    public void testJsonToJavaBean() {
        String jsonString = "{\"bir\":1619330606019,\"id\":1,\"salary\":10000.0,\"userName\":\"userName--1\"}";
        Gson gson = getGson();
        DemoOne demoOne = gson.fromJson(jsonString, DemoOne.class);
        System.out.println("json字符窜转为java bean");
        System.out.println(demoOne);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

报错信息

com.google.gson.JsonSyntaxException: 1619330606019

at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:87)
at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:75)
at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:46)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory 1. r e a d ( R e f l e c t i v e T y p e A d a p t e r F a c t o r y . j a v a : 131 ) a t c o m . g o o g l e . g s o n . i n t e r n a l . b i n d . R e f l e c t i v e T y p e A d a p t e r F a c t o r y 1.read(ReflectiveTypeAdapterFactory.java:131) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory 1.read(ReflectiveTypeAdapterFactory.java:131)atcom.google.gson.internal.bind.ReflectiveTypeAdapterFactoryAdapter.read(ReflectiveTypeAdapterFactory.java:222)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.google.gson.Gson.fromJson(Gson.java:813)
at com.example.demo.json.GsonTest.testJsonToJavaBean(GsonTest.java:132)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:686)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain V a l i d a t i n g I n v o c a t i o n . p r o c e e d ( I n v o c a t i o n I n t e r c e p t o r C h a i n . j a v a : 131 ) a t o r g . j u n i t . j u p i t e r . e n g i n e . e x t e n s i o n . T i m e o u t E x t e n s i o n . i n t e r c e p t ( T i m e o u t E x t e n s i o n . j a v a : 149 ) a t o r g . j u n i t . j u p i t e r . e n g i n e . e x t e n s i o n . T i m e o u t E x t e n s i o n . i n t e r c e p t T e s t a b l e M e t h o d ( T i m e o u t E x t e n s i o n . j a v a : 140 ) a t o r g . j u n i t . j u p i t e r . e n g i n e . e x t e n s i o n . T i m e o u t E x t e n s i o n . i n t e r c e p t T e s t M e t h o d ( T i m e o u t E x t e n s i o n . j a v a : 84 ) a t o r g . j u n i t . j u p i t e r . e n g i n e . e x e c u t i o n . E x e c u t a b l e I n v o k e r ValidatingInvocation.proceed(InvocationInterceptorChain.java:131) at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149) at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140) at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84) at org.junit.jupiter.engine.execution.ExecutableInvoker ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)atorg.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)atorg.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)atorg.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)atorg.junit.jupiter.engine.execution.ExecutableInvokerReflectiveInterceptorCall.lambda$ofVoidMethod 0 ( E x e c u t a b l e I n v o k e r . j a v a : 115 ) a t o r g . j u n i t . j u p i t e r . e n g i n e . e x e c u t i o n . E x e c u t a b l e I n v o k e r . l a m b d a 0(ExecutableInvoker.java:115) at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda 0(ExecutableInvoker.java:115)atorg.junit.jupiter.engine.execution.ExecutableInvoker.lambdainvoke 0 ( E x e c u t a b l e I n v o k e r . j a v a : 105 ) a t o r g . j u n i t . j u p i t e r . e n g i n e . e x e c u t i o n . I n v o c a t i o n I n t e r c e p t o r C h a i n 0(ExecutableInvoker.java:105) at org.junit.jupiter.engine.execution.InvocationInterceptorChain 0(ExecutableInvoker.java:105)atorg.junit.jupiter.engine.execution.InvocationInterceptorChainInterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod 6 ( T e s t M e t h o d T e s t D e s c r i p t o r . j a v a : 212 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . T h r o w a b l e C o l l e c t o r . e x e c u t e ( T h r o w a b l e C o l l e c t o r . j a v a : 73 ) a t o r g . j u n i t . j u p i t e r . e n g i n e . d e s c r i p t o r . T e s t M e t h o d T e s t D e s c r i p t o r . i n v o k e T e s t M e t h o d ( T e s t M e t h o d T e s t D e s c r i p t o r . j a v a : 208 ) a t o r g . j u n i t . j u p i t e r . e n g i n e . d e s c r i p t o r . T e s t M e t h o d T e s t D e s c r i p t o r . e x e c u t e ( T e s t M e t h o d T e s t D e s c r i p t o r . j a v a : 137 ) a t o r g . j u n i t . j u p i t e r . e n g i n e . d e s c r i p t o r . T e s t M e t h o d T e s t D e s c r i p t o r . e x e c u t e ( T e s t M e t h o d T e s t D e s c r i p t o r . j a v a : 71 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . N o d e T e s t T a s k . l a m b d a 6(TestMethodTestDescriptor.java:212) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:208) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda 6(TestMethodTestDescriptor.java:212)atorg.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)atorg.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:208)atorg.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137)atorg.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)atorg.junit.platform.engine.support.hierarchical.NodeTestTask.lambdaexecuteRecursively 5 ( N o d e T e s t T a s k . j a v a : 135 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . T h r o w a b l e C o l l e c t o r . e x e c u t e ( T h r o w a b l e C o l l e c t o r . j a v a : 73 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . N o d e T e s t T a s k . l a m b d a 5(NodeTestTask.java:135) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda 5(NodeTestTask.java:135)atorg.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)atorg.junit.platform.engine.support.hierarchical.NodeTestTask.lambdaexecuteRecursively 7 ( N o d e T e s t T a s k . j a v a : 125 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . N o d e . a r o u n d ( N o d e . j a v a : 135 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . N o d e T e s t T a s k . l a m b d a 7(NodeTestTask.java:125) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda 7(NodeTestTask.java:125)atorg.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)atorg.junit.platform.engine.support.hierarchical.NodeTestTask.lambdaexecuteRecursively 8 ( N o d e T e s t T a s k . j a v a : 123 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . T h r o w a b l e C o l l e c t o r . e x e c u t e ( T h r o w a b l e C o l l e c t o r . j a v a : 73 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . N o d e T e s t T a s k . e x e c u t e R e c u r s i v e l y ( N o d e T e s t T a s k . j a v a : 122 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . N o d e T e s t T a s k . e x e c u t e ( N o d e T e s t T a s k . j a v a : 80 ) a t j a v a . u t i l . A r r a y L i s t . f o r E a c h ( A r r a y L i s t . j a v a : 1249 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . S a m e T h r e a d H i e r a r c h i c a l T e s t E x e c u t o r S e r v i c e . i n v o k e A l l ( S a m e T h r e a d H i e r a r c h i c a l T e s t E x e c u t o r S e r v i c e . j a v a : 38 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . N o d e T e s t T a s k . l a m b d a 8(NodeTestTask.java:123) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) at java.util.ArrayList.forEach(ArrayList.java:1249) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda 8(NodeTestTask.java:123)atorg.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)atorg.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)atorg.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)atjava.util.ArrayList.forEach(ArrayList.java:1249)atorg.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)atorg.junit.platform.engine.support.hierarchical.NodeTestTask.lambdaexecuteRecursively 5 ( N o d e T e s t T a s k . j a v a : 139 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . T h r o w a b l e C o l l e c t o r . e x e c u t e ( T h r o w a b l e C o l l e c t o r . j a v a : 73 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . N o d e T e s t T a s k . l a m b d a 5(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda 5(NodeTestTask.java:139)atorg.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)atorg.junit.platform.engine.support.hierarchical.NodeTestTask.lambdaexecuteRecursively 7 ( N o d e T e s t T a s k . j a v a : 125 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . N o d e . a r o u n d ( N o d e . j a v a : 135 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . N o d e T e s t T a s k . l a m b d a 7(NodeTestTask.java:125) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda 7(NodeTestTask.java:125)atorg.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)atorg.junit.platform.engine.support.hierarchical.NodeTestTask.lambdaexecuteRecursively 8 ( N o d e T e s t T a s k . j a v a : 123 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . T h r o w a b l e C o l l e c t o r . e x e c u t e ( T h r o w a b l e C o l l e c t o r . j a v a : 73 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . N o d e T e s t T a s k . e x e c u t e R e c u r s i v e l y ( N o d e T e s t T a s k . j a v a : 122 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . N o d e T e s t T a s k . e x e c u t e ( N o d e T e s t T a s k . j a v a : 80 ) a t j a v a . u t i l . A r r a y L i s t . f o r E a c h ( A r r a y L i s t . j a v a : 1249 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . S a m e T h r e a d H i e r a r c h i c a l T e s t E x e c u t o r S e r v i c e . i n v o k e A l l ( S a m e T h r e a d H i e r a r c h i c a l T e s t E x e c u t o r S e r v i c e . j a v a : 38 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . N o d e T e s t T a s k . l a m b d a 8(NodeTestTask.java:123) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) at java.util.ArrayList.forEach(ArrayList.java:1249) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda 8(NodeTestTask.java:123)atorg.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)atorg.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)atorg.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)atjava.util.ArrayList.forEach(ArrayList.java:1249)atorg.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)atorg.junit.platform.engine.support.hierarchical.NodeTestTask.lambdaexecuteRecursively 5 ( N o d e T e s t T a s k . j a v a : 139 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . T h r o w a b l e C o l l e c t o r . e x e c u t e ( T h r o w a b l e C o l l e c t o r . j a v a : 73 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . N o d e T e s t T a s k . l a m b d a 5(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda 5(NodeTestTask.java:139)atorg.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)atorg.junit.platform.engine.support.hierarchical.NodeTestTask.lambdaexecuteRecursively 7 ( N o d e T e s t T a s k . j a v a : 125 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . N o d e . a r o u n d ( N o d e . j a v a : 135 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . N o d e T e s t T a s k . l a m b d a 7(NodeTestTask.java:125) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda 7(NodeTestTask.java:125)atorg.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)atorg.junit.platform.engine.support.hierarchical.NodeTestTask.lambdaexecuteRecursively 8 ( N o d e T e s t T a s k . j a v a : 123 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . T h r o w a b l e C o l l e c t o r . e x e c u t e ( T h r o w a b l e C o l l e c t o r . j a v a : 73 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . N o d e T e s t T a s k . e x e c u t e R e c u r s i v e l y ( N o d e T e s t T a s k . j a v a : 122 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . N o d e T e s t T a s k . e x e c u t e ( N o d e T e s t T a s k . j a v a : 80 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . S a m e T h r e a d H i e r a r c h i c a l T e s t E x e c u t o r S e r v i c e . s u b m i t ( S a m e T h r e a d H i e r a r c h i c a l T e s t E x e c u t o r S e r v i c e . j a v a : 32 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . H i e r a r c h i c a l T e s t E x e c u t o r . e x e c u t e ( H i e r a r c h i c a l T e s t E x e c u t o r . j a v a : 57 ) a t o r g . j u n i t . p l a t f o r m . e n g i n e . s u p p o r t . h i e r a r c h i c a l . H i e r a r c h i c a l T e s t E n g i n e . e x e c u t e ( H i e r a r c h i c a l T e s t E n g i n e . j a v a : 51 ) a t o r g . j u n i t . p l a t f o r m . l a u n c h e r . c o r e . D e f a u l t L a u n c h e r . e x e c u t e ( D e f a u l t L a u n c h e r . j a v a : 248 ) a t o r g . j u n i t . p l a t f o r m . l a u n c h e r . c o r e . D e f a u l t L a u n c h e r . l a m b d a 8(NodeTestTask.java:123) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32) at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248) at org.junit.platform.launcher.core.DefaultLauncher.lambda 8(NodeTestTask.java:123)atorg.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)atorg.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)atorg.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)atorg.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)atorg.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)atorg.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)atorg.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)atorg.junit.platform.launcher.core.DefaultLauncher.lambdaexecute 5 ( D e f a u l t L a u n c h e r . j a v a : 211 ) a t o r g . j u n i t . p l a t f o r m . l a u n c h e r . c o r e . D e f a u l t L a u n c h e r . w i t h I n t e r c e p t e d S t r e a m s ( D e f a u l t L a u n c h e r . j a v a : 226 ) a t o r g . j u n i t . p l a t f o r m . l a u n c h e r . c o r e . D e f a u l t L a u n c h e r . e x e c u t e ( D e f a u l t L a u n c h e r . j a v a : 199 ) a t o r g . j u n i t . p l a t f o r m . l a u n c h e r . c o r e . D e f a u l t L a u n c h e r . e x e c u t e ( D e f a u l t L a u n c h e r . j a v a : 132 ) a t c o m . i n t e l l i j . j u n i t 5. J U n i t 5 I d e a T e s t R u n n e r . s t a r t R u n n e r W i t h A r g s ( J U n i t 5 I d e a T e s t R u n n e r . j a v a : 69 ) a t c o m . i n t e l l i j . r t . e x e c u t i o n . j u n i t . I d e a T e s t R u n n e r 5(DefaultLauncher.java:211) at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132) at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69) at com.intellij.rt.execution.junit.IdeaTestRunner 5(DefaultLauncher.java:211)atorg.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)atorg.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)atorg.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)atcom.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)atcom.intellij.rt.execution.junit.IdeaTestRunnerRepeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.text.ParseException: Failed to parse date [“1619330606019”]: Invalid time zone indicator ‘0’
at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:274)
at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:85)
… 72 more
Caused by: java.lang.IndexOutOfBoundsException: Invalid time zone indicator ‘0’
at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:245)
… 73 more

解决方案

运行到gson解析JSON时报如上异常,代码没变,为什么在不同服务器上运行的结果就不一样了呢,后来各种搜索,才了解到Gson不同服务器在解析时间格式不一样,了解到这一点后,只需要在Gson实例化指定解析的格式即可,将

Gson gson=new Gson();替换为 Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

最终代码实现

对应测试的javabean

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class DemoOne {
    private Integer id;
    private String userName;
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date bir;
    private Double salary;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

测试代码

 @Test
    public void testJavaBeanToJson() {
        DemoOne demoOne = new DemoOne().setBir(new Date()).setId(1).setSalary(10000.0).setUserName("张三");
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); //按照 yyyy-MM-dd HH:mm:ss格式化。Gson
        /**
         * 如果gson不进行时间格式化处理可能会报错
         * com.google.gson.JsonSyntaxException
         */
//        Gson gson = new Gson();
        String jsonString = gson.toJson(demoOne);
        System.out.println("java bean转为json格式字符窜");
        System.out.println(jsonString);
    }
    /*
    控制台打印:
     java bean转为json格式字符窜
{"id":1,"userName":"张三","bir":"2021-04-25 15:40:31","salary":10000.0}
     */

    /**
     * json格式字符窜转为 java bean
     */
    @Test
    public void testJsonToJavaBean() {
        String jsonString = "{\"id\":1,\"userName\":\"张三\",\"bir\":\"2021-04-25 15:36:49\",\"salary\":10000.0}";
        Gson gson = getGson();
        DemoOne demoOne = gson.fromJson(jsonString, DemoOne.class);
        System.out.println("json字符窜转为java bean");
        System.out.println(demoOne);
    }
/*
json字符窜转为java bean
DemoOne(id=1, userName=张三, bir=Sun Apr 25 15:36:49 CST 2021, salary=10000.0)
 */
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/246308
推荐阅读
相关标签
  

闽ICP备14008679号