У меня очень простой POJO:
Код: Выделить всё
public class Foo {
private int important;
private String something;
//constructors, getter, seters, toString
}
Код: Выделить всё
@SpringBootApplication
@RestController
public class QuestionController {
public static void main(String[] args) {
SpringApplication.run(QuestionController.class, args);
}
@GetMapping(value = "/question")
Mono question(@RequestBody Foo foo) {
System.out.println("The object foo, with value for important = " + foo.getImportant() + " and something = " + foo.getSomething());
return Mono.just("question");
}
}
Код: Выделить всё
{
"important": 42,
"something": "value"
}
Однако, если есть опечатка: (обратите внимание на опечатку в слове «важно»)
Код: Выделить всё
{
"importantWithTypo": 42,
"something": "value"
}
Код: Выделить всё
{
"something": "value"
}
Я не хочу, чтобы Spring по умолчанию имел значение 0 и думал, что все в порядке.
Я также не хочу менять свои типы с примитивов на упакованный объект.
Без меня пишу что-то вроде:
Код: Выделить всё
@GetMapping(value = "/question")
Mono question(@RequestBody Foo foo) {
if (0 == foo.getImportant()) {
throw new IllegalArgumentException();
}
System.out.println("The object foo, with value for important = " + foo.getImportant() + " and something = " + foo.getSomething());
return Mono.just("question");
}
Спасибо
Подробнее здесь: https://stackoverflow.com/questions/645 ... equestbody
Мобильная версия