Ranvijay Singh
Ranvijay Singh

Reputation: 21

Error in Deserializing json string comming through a rest webservice call

Having a webservise which returns an appfuse User instance in the form of json, where this user model contains a property roles which is a set of role objects. My problem is that when this roles property contains no role object or more than one role objects it work fine, but when it contains single role element, it is not working

Exception::

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 218
   at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:180)
   at com.google.gson.Gson.fromJson(Gson.java:755)
   at com.google.gson.Gson.fromJson(Gson.java:721)
   at com.google.gson.Gson.fromJson(Gson.java:670)
   at com.google.gson.Gson.fromJson(Gson.java:642)
   at com.steriamobile.ws.smfserver.service.SMFUserServiceImpl.getUser(SMFUserServiceImpl.java:44)
   at com.steriamobile.ws.smfserver.service.SMFUserManagerImpl.getUser(SMFUserManagerImpl.java:19)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
   at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
   at $Proxy32.getUser(Unknown Source)

REST Service::

 @GET
@Path("/getUser/{id}")
public User get(@PathParam("id") Long id) {
    return userManager.get(id);
}

Deserializing Code:

Gson gson = new Gson();     
User user = gson.fromJson(json,new TypeToken<User>() {}.getType());

Upvotes: 0

Views: 404

Answers (1)

Programmer Bruce
Programmer Bruce

Reputation: 66943

As I understand the problem description, custom deserialization is necessary to handle the situation where the JSON is sometimes an array and sometimes an object.

Using Gson to handle this specific issue has been covered in previous StackOverflow threads, such as Parsing JSON with GSON, object sometimes contains list sometimes contains object and Gson handle object or array.

Upvotes: 1

Related Questions