Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用@JsonTypeInfo&;将JSON反序列化为多态对象模型@JsonSubTypes不工作?_Java_Json_Spring_Jackson - Fatal编程技术网

Java 使用@JsonTypeInfo&;将JSON反序列化为多态对象模型@JsonSubTypes不工作?

Java 使用@JsonTypeInfo&;将JSON反序列化为多态对象模型@JsonSubTypes不工作?,java,json,spring,jackson,Java,Json,Spring,Jackson,我试图让REST端点在POSTing时创建Widget的子类型 下面是所有小部件的基类 @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "widgetType") @JsonSubTypes({ @JsonSubTypes.Type(value = TextWidget.class, name = WidgetType.Constants.TEXT),

我试图让REST端点在
POST
ing时创建
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
enum:

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) {
    ...
    }
略读一下我的问题的几个解决方案,我可能不得不手动注册子类型,我可能错了,但我认为一定有办法让它与注释一起工作,也许我遗漏了什么?

问题解决了, 我用Jackson注释来注释我的类,但忘记了我发送的是多部分POST请求,这甚至不会发送给Jackson。 解决方案如下所示:

@RequestMapping(method = RequestMethod.POST)
public Optional<Widget> createWidget(@RequestBody final Widget widget) {
    ...
    }
@RequestMapping(method=RequestMethod.POST)
公共可选createWidget(@RequestBody-final-Widget-Widget){
...
}
问题已解决, 我用Jackson注释来注释我的类,但忘记了我发送的是多部分POST请求,这甚至不会发送给Jackson。 解决方案如下所示:

@RequestMapping(method = RequestMethod.POST)
public Optional<Widget> createWidget(@RequestBody final Widget widget) {
    ...
    }
@RequestMapping(method=RequestMethod.POST)
公共可选createWidget(@RequestBody-final-Widget-Widget){
...
}