My objectmapper not working when I use spring controller & class for requestbody inheritation .
@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);
}
and also
@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. I have BaseController for diets and recipes
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);
}
}
and some realizations. example for diet
@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);
}
}
But when I send json with info, days, type ('diet') to '/diet/1/version/last' then I see in debug mode that my DietVersion pure clear and has no any data. Why ?
How to change settings for objectmapper ?
CodePudding user response:
what if you provide all this in your DietController class.
public void saveVersion(@PathVariable("docId") String docId, @RequestBody V version, Authentication authentication){