我有一个JSON数据,它通过REST WebService:传递
"dimension": {
"@type": "com.example.multi.MultiDimension",
"listId": "tempListId1",
"key": "PERN",
"displayName": "Personal"
}
现在,MultiDimension是一个Singleton类,即具有私有构造函数。所以Jackson找不到公共构造函数,并给出了解析错误。
No suitable constructor found for type [simple type, class com.example.multi.MultiDimension]: can not instantiate from JSON object (need to add/enable type information?)n at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1@404e8aba; line: 8, column: 9]
好吧,然后我在MultiDimension类中添加了一个公共构造函数,正如预期的那样,它起作用了。
我担心的是,从我打开的Singleton类中,我仍然可以有一个Singleton class,Jackson Mapper可以映射指定方法名的类,即来自类的getInstance()
您可以使用@jsonCreator,paramtere可以像这样传递。
@JsonCreator
public static MultiDimension getInstance(@JsonProperty("key") String key) {...}