Я использую аннотацию Джексона @JsonUnwrapped, чтобы сгладить свой запрос, одновременно используя наследование и универсальные типы.
Дело в том, что что я не хочу использовать собственный десериализатор.
Этап воспроизведения:
Код: Выделить всё
{
"bar1": "12",
"bar2": "bar2",
"foo1": "foo1",
"foo2": "123"
}
Код: Выделить всё
@Slf4j
@Validated
@RestController
@RequestMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
public class MyController {
@PostMapping("/bar")
public void test(@Valid @RequestBody final Bar1 bar1) {
log.info("{}", bar1);
}
}
Код: Выделить всё
public abstract class AbstractBar {
@Pattern(regexp = "\\d{2}")
protected final String bar1;
@Valid
@JsonUnwrapped
protected final F foo;
protected AbstractBar(final String bar1, final F foo) {
this.bar1 = bar1;
this.foo = foo;
}
public String getBar1() {
return this.bar1;
}
public F getFoo() {
return this.foo;
}
}
Код: Выделить всё
public abstract class AbstractFoo {
protected final String foo1;
@Pattern(regexp = "\\d{3}")
protected final String foo2;
protected AbstractFoo(final String foo1,
final String foo2) {
this.foo1 = foo1;
this.foo2 = foo2;
}
public String getFoo1() {
return this.foo1;
}
public String getFoo2() {
return this.foo2;
}
}
Код: Выделить всё
public class Bar1 extends AbstractBar {
@NotBlank
private final String bar2;
@JsonCreator
public Bar1(final String bar1,
final String bar2,
final String foo1,
final String foo2) {
super(bar1, new Foo1(foo1, foo2));
this.bar2 = bar2;
}
@Override
public @NotBlank String getBar1() {
return this.bar1;
}
@Override
public @NotNull Foo1 getFoo() {
return this.foo;
}
}
Код: Выделить всё
public class Foo1 extends AbstractFoo {
public Foo1(final String foo1,
final String foo2) {
super(foo1, foo2);
}
@Override
public @NotBlank String getFoo1() {
return this.foo1;
}
@Override
public @Nullable String getFoo2() {
return this.foo2;
}
}
[img]https://i .sstatic.net/iD56A4j8.png[/img]
Но, введя геттер, я получаю нулевые значения
, что заканчивается ошибкой проверки ограничения через мой Foo1 @NotBlank в поле foo1

Я использую Spring Boot 3.4.0, что означает зависимости от jackson 2.18.1.
Большое спасибо за уделенное время ❤
Подробнее здесь: https://stackoverflow.com/questions/792 ... nheritance
Мобильная версия