0
我想有一個REST端點創建Widget
時POST
荷蘭國際集團給它一個亞型,使用@JsonTypeInfo&@JsonSubTypes不能正常工作將JSON反序列化爲多態對象模型?
這裏是所有Widget
小號
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "widgetType")
@JsonSubTypes({
@JsonSubTypes.Type(value = TextWidget.class, name = WidgetType.Constants.TEXT),
@JsonSubTypes.Type(value = ImageWidget.class, name = WidgetType.Constants.IMAGE),
@JsonSubTypes.Type(value = IndicatorWidget.class, name = WidgetType.Constants.INDICATOR),
@JsonSubTypes.Type(value = MapWidget.class, name = WidgetType.Constants.MAP),
@JsonSubTypes.Type(value = ChartWidget.class, name = WidgetType.Constants.CHART)
})
@Data
@Slf4j
public abstract class Widget {
...
}
的基類,這是在WidgetType
枚舉:
public enum WidgetType {
TEXT(Constants.TEXT),
IMAGE(Constants.IMAGE),
INDICATOR(Constants.INDICATOR),
MAP(Constants.MAP),
CHART(Constants.CHART);
private final String type;
WidgetType(final String type) {
this.type = type;
}
public static class Constants {
public static final String TEXT = "TEXT";
public static final String IMAGE = "IMAGE";
public static final String INDICATOR = "INDICATOR";
public static final String MAP = "MAP";
public static final String CHART = "CHART";
}
}
這是我的春天端點:
@RequestMapping(method = RequestMethod.POST)
public Optional<Widget> createWidget(@Valid final Widget widget) {
...
}
打端點時,它拋出這個異常:
{
"timestamp": 1493029336774,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.beans.BeanInstantiationException",
"message": "Failed to instantiate [....models.Widget]: Is it an abstract class?; nested exception is java.lang.InstantiationException",
"path": "...."
}
通過我的問題幾種解決方案略讀,我可能要手動註冊亞型,我可能是錯的,但我認爲必須有一個辦法使它與註釋一起工作,也許我錯過了一些東西?