Разве этого не должно быть достаточно чтобы установить AnyOf = { PolarParrot.class, NorwegianParrot.class}, DiscriminatorProperty = "parrotType", чтобы ввести подсказку, какой конкретный экземпляр Parrot должен использоваться для разрешения RequestBody?
Код: Выделить всё
import java.lang.Override;
import java.net.URI;
import java.util.Map;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
@RestController
public class PolyParrot {
@PostMapping(value = {"/create"}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity create(@Valid @RequestBody Flock flock) {
return ResponseEntity.created(URI.create("/flock/42")).build();
}
}
@Schema(additionalProperties = Schema.AdditionalPropertiesValue.TRUE)
class Flock {
@Schema
Map birds;
public Map getBirds() {
return birds;
}
public void setBirds(Map birds) {
this.birds = birds;
}
}
@Schema(anyOf = {
PolarParrot.class,
NorwegianParrot.class,
}, discriminatorProperty = "parrotType")
interface Parrot {
String getParrotType();
String getPlummage();
}
@Schema()
class PolarParrot implements Parrot {
@Schema(requiredMode = Schema.RequiredMode.REQUIRED, defaultValue = "polar", example = "polar")
String parrotType;
@Override
public String getParrotType() {
return parrotType;
}
@Override
public String getPlummage() {
return "red";
}
}
@Schema()
class NorwegianParrot implements Parrot {
@Schema(requiredMode = Schema.RequiredMode.REQUIRED, defaultValue = "norwegian", example = "norwegian")
String parrotType;
@Override
public String getParrotType() {
return parrotType;
}
@Override
public String getPlummage() {
return "blue";
}
}
code> объект, который он выдает:
Код: Выделить всё
com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Cannot construct instance of `Parrot`
(no Creators, like default constructor, exist):
abstract types either need to be mapped to concrete types,
have custom deserializer, or contain additional type information
Какую важную часть я здесь пропустил?
Подробнее здесь: https://stackoverflow.com/questions/791 ... orproperty