Die Deserialisierung von Spring Boot 4 Jackson verhält sich andersJava

Java-Forum
Anonymous
 Die Deserialisierung von Spring Boot 4 Jackson verhält sich anders

Post by Anonymous »

Ich habe ein Spring Boot 4-Projekt mit dem folgenden Controller und DTO.

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;
}
}
Die folgende Beitragsanfrage wurde erfolgreich abgeschlossen

Code: Select all

{
"id": 101,
"name": "John Doe",
"age": 34
}
Die folgende Postanforderung schlägt mit der folgenden Ausnahme fehl

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)]
Es beschwert sich über ein primitives Feld, das nicht null sein darf, aber warum verwendet es überhaupt den parametrisierten Konstruktor anstelle des Konstruktors ohne Argumente?
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.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post