Spring 控制器继承和链接请求体反序列化问题

Spring controller inheritance and linked requestbody deserealization problems

当我使用 spring 控制器和 class 进行请求体继承时,我的 objectmapper 不工作。

 @JsonTypeInfo(
            use = JsonTypeInfo.Id.NAME,
            include = JsonTypeInfo.As.PROPERTY,
            property = "type", visible = true)
    @JsonSubTypes({
            @JsonSubTypes.Type(value = RecipeVersion.class, name = "recipe"),
            @JsonSubTypes.Type(value = DietVersion.class, name = "diet"),
    })
    public interface DocumentVersion {
        Info getInfo();
        void setInfo(Info info);
    }

还有

@Data
public class DietVersion implements DocumentVersion {
    private LocalizedText warnings;
    private List<DietDay> days = new LinkedList<>();
    private Info info = new Info();

    private String getType() {
        return "diet";
    }
}

好的。我有用于饮食和食谱的 BaseController

abstract public class BaseController<T extends Document<V>, V extends DocumentVersion> {

    abstract protected BaseService<T, V> getService();

    @PostMapping("/{docId}/version/last")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void saveVersion(@PathVariable("docId") String docId, @RequestBody V version, Authentication authentication) {
        getService().replaceLastVersion(docId, version, authentication);
    }
}

和一些认识。饮食示例

@Controller
@RequestMapping("/diet")
public class DietController extends BaseController<Diet, DietVersion> {

    private final DietService dietService;

    @Autowired
    public DietController(DietService dietService) {
        this.dietService = dietService;
    }

    @Override
    protected DietService getService() {
        return dietService;
    }

    @Override
    public void saveVersion(String docId, DietVersion version, Authentication authentication) {
        super.saveVersion(docId, version, authentication);
    }
}

但是当我发送 json 和信息、天数、类型 ('diet') 到 '/diet/1/version/last' 时,我在调试模式下看到我的 DietVersion 完全清晰并且没有任何数据。为什么?

如何更改 objectmapper 的设置?

如果您在 DietController 中提供所有这些内容会怎么样 class。

public void saveVersion(@PathVariable("docId") String docId, @RequestBody V version, Authentication authentication){