मैं com.fasterxml.jackson.databind.ObjectMapper का उपयोग करके एक अपरिवर्तनीय वस्तु को क्रमबद्ध और निष्क्रिय करना चाहता हूं।
अपरिवर्तनीय वर्ग इस तरह दिखता है (सिर्फ 3 आंतरिक गुण, गेटर्स और कंस्ट्रक्टर):
public final class ImportResultItemImpl implements ImportResultItem {
private final ImportResultItemType resultType;
private final String message;
private final String name;
public ImportResultItemImpl(String name, ImportResultItemType resultType, String message) {
super();
this.resultType = resultType;
this.message = message;
this.name = name;
}
public ImportResultItemImpl(String name, ImportResultItemType resultType) {
super();
this.resultType = resultType;
this.name = name;
this.message = null;
}
@Override
public ImportResultItemType getResultType() {
return this.resultType;
}
@Override
public String getMessage() {
return this.message;
}
@Override
public String getName() {
return this.name;
}
}
हालाँकि जब मैं यह इकाई परीक्षण चलाता हूँ:
@Test
public void testObjectMapper() throws Exception {
ImportResultItemImpl originalItem = new ImportResultItemImpl("Name1", ImportResultItemType.SUCCESS);
String serialized = new ObjectMapper().writeValueAsString((ImportResultItemImpl) originalItem);
System.out.println("serialized: " + serialized);
//this line will throw exception
ImportResultItemImpl deserialized = new ObjectMapper().readValue(serialized, ImportResultItemImpl.class);
}
मुझे यह अपवाद मिलता है:
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class eu.ibacz.pdkd.core.service.importcommon.ImportResultItemImpl]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: {"resultType":"SUCCESS","message":null,"name":"Name1"}; line: 1, column: 2]
at
... nothing interesting here
यह अपवाद मुझे एक डिफ़ॉल्ट निर्माणकर्ता बनाने के लिए कहता है, लेकिन यह एक अपरिवर्तनीय वस्तु है, इसलिए मैं ऐसा नहीं करना चाहता। यह आंतरिक विशेषताओं को कैसे निर्धारित करेगा? यह पूरी तरह से एपीआई के उपयोगकर्ता को भ्रमित करेगा।
तो मेरा सवाल यह है: क्या मैं किसी भी तरह डिफ़ॉल्ट ऑब्जेक्ट्स के बिना अपरिवर्तनीय वस्तुओं को क्रमबद्ध / अनुक्रमित कर सकता हूं?