Reputation: 5845
I implemented a RESTful web service with Spring and am using Jackson JSON as the serializer / deserializer for JSON objects.
However I run into Error 415's when the object that is to be deserialized contains a HashMap:
private Map<String, String> requestMap = new HashMap<String, String>();
If I remove this, everything works perfectly. Is this a known issue? Are there any fixes?
Thanks, Sri
Upvotes: 1
Views: 3911
Reputation: 66943
Strictly speaking, Jackson serializes from Interface-type references just fine. The following demonstrates this point.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
public class Foo
{
public static void main(String[] args) throws Exception
{
ObjectMapper mapper = new ObjectMapper();
Map<String, String> requestMap = new HashMap<String, String>();
requestMap.put("one", "1");
requestMap.put("two", "2");
System.out.println(mapper.writeValueAsString(requestMap));
// output: {"two":"2","one":"1"}
List<UserPermission> userPermissions = new ArrayList<UserPermission>();
userPermissions.add(new UserPermissionImpl("domain1"));
userPermissions.add(new UserPermissionImpl("domain2"));
System.out.println(mapper.writeValueAsString(userPermissions));
// output: [{"scope":"domain1"},{"scope":"domain2"}]
Container container = new ContainerImpl(requestMap, userPermissions);
// From an Interface-type reference, where the implementation is an object with two Interface-type references:
System.out.println(mapper.writeValueAsString(container));
// {"requestMap":{"two":"2","one":"1"},"userPermissions":[{"scope":"domain1"},{"scope":"domain2"}]}
}
}
interface UserPermission {}
class UserPermissionImpl implements UserPermission
{
public String scope;
UserPermissionImpl(String scope) { this.scope = scope; }
}
interface Container {}
class ContainerImpl implements Container
{
public Map<String, String> requestMap;
public List<UserPermission> userPermissions;
ContainerImpl(Map<String, String> requestMap, List<UserPermission> userPermissions)
{ this.requestMap = requestMap; this.userPermissions = userPermissions; }
}
There's some other problem in the system you're using.
Upvotes: 1
Reputation: 5845
Discovered the problem. Jackson JSON has difficulties with Interfaces. So in the definitions, use HashMaps and ArrayLists instead of Maps and Lists. Not sure if this is a perfect solution, but it works for me.
Upvotes: 0