Код: Выделить всё
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PersonController {
@PostMapping("/persons")
public PersonDto create(@RequestBody PersonDto personDto) {
System.out.println(personDto);
return personDto;
}
}
Код: Выделить всё
public class PersonDto {
private int id;
private String name;
private int age;
public PersonDto() {
}
public PersonDto(final int id, final String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(final int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(final int age) {
this.age = age;
}
}
Код: Выделить всё
{
"id": 101,
"name": "John Doe",
"age": 34
}
Код: Выделить всё
{
"id": 101,
"name": "John Doe"
}
Код: Выделить всё
2025-12-24T18:53:07.979+05:30 WARN 46624 --- [dummy] [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot map `null` into type `int` (set DeserializationConfig.DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES to 'false' to allow)]
Примечание. Тот же запрос работает, если я удалю параметризованный конструктор и оставлю только непараметризованный конструктор. Похоже, он всегда пытается использовать параметризованный конструктор, даже не упомянутый конкретно.
Подробнее здесь: https://stackoverflow.com/questions/798 ... ifferently
Мобильная версия