我在使用来自 json 的 Jackson 序列化时遇到问题,如何从Collections.unmodifiableMap?
我得到的错误是:
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.util.Collections$UnmodifiableMap, problem: No default constructor found
我想使用SimpleAbstractTypeResolver
来自http://wiki.fasterxml.com/SimpleAbstractTypeResolver但是我无法获得内部类类型Collections$UnmodifiableMap
Map<Integer, String> emailMap = newHashMap();
Account testAccount = new Account();
ObjectMapper mapper = new ObjectMapper();
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, As.PROPERTY);
String marshalled ;
emailMap.put(Integer.valueOf(10), "bob@mail.com");
testAccount.setMemberEmails(emailMap);
marshalled = mapper.writeValueAsString(testAccount);
System.out.println(marshalled);
Account returnedAccount = mapper.readValue(marshalled, Account.class);
System.out.println(returnedAccount.containsValue("bob@mail.com"));
public class Account {
private Map<Integer, String> memberEmails = Maps.newHashMap();
public void setMemberEmails(Map<Integer, String> memberEmails) {
this.memberEmails = memberEmails;
}
public Map<Integer, String> getMemberEmails() {
return Collections.unmodifiableMap(memberEmails);
}
有任何想法吗?提前致谢。