Code: Select all
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;
}
}
Code: Select all
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;
}
}
Code: Select all
{
"id": 101,
"name": "John Doe",
"age": 34
}
Code: Select all
{
"id": 101,
"name": "John Doe"
}
Code: Select all
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)]
Hinweis: Die gleiche Anfrage funktioniert, wenn ich den parametrisierten Konstruktor entferne und nur den Konstruktor ohne Argumente habe. Es scheint, als würde versucht, immer den parametrisierten Konstruktor zu verwenden, auch wenn er nicht ausdrücklich erwähnt wird.
Mobile version