我使用JSON從傑克遜序列化問題,如何從Collections.unmodifiableMap?
傑克遜序列化Collections.unmodifiable *
我得到的錯誤序列是:
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), "[email protected]");
testAccount.setMemberEmails(emailMap);
marshalled = mapper.writeValueAsString(testAccount);
System.out.println(marshalled);
Account returnedAccount = mapper.readValue(marshalled, Account.class);
System.out.println(returnedAccount.containsValue("[email protected]"));
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);
}
任何想法?提前致謝。
感謝感知。這完全符合我的目的。是否有一種方法可以在模型類本身沒有回退註釋的情況下具有相同的行爲? – hartraft
@hartraft您可能想要使用的是Jackson Mix In的http://wiki.fasterxml.com/JacksonMixInAnnotations – leo