当我使用弹簧控制器时,我的objectmapper不工作&类用于requestbody继承。
@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";
}
}
Ok。我对饮食和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类中提供所有这些呢?
public void saveVersion(@PathVariable("docId") String docId, @RequestBody V version, Authentication authentication){