나는 REST 끝점의 하위 유형 만들도록하려고 Widget
할 때 POST
, 그것을 보내고을
여기에 모든 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";
}
}
그리고 이것은 내 Spring 끝점입니다.
@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": "...."
}
내 문제에 대한 몇 가지 해결책을 훑어 보면서 수동으로 하위 유형을 등록해야 할 수도 있고 틀릴 수도 있지만 주석과 함께 작동하도록 만드는 방법이 있어야한다고 생각합니다.
문제가 해결되어 Jackson 주석으로 클래스에 주석을 달았고 Jackson에게도 가지 않는 다중 POST 요청을 보내는 것을 잊었습니다. 해결책은 다음과 같이 간단합니다.
@RequestMapping(method = RequestMethod.POST)
public Optional<Widget> createWidget(@RequestBody final Widget widget) {
...
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다