Spring Boot: 400 Bad Request beim Senden einer JSON-POST-Anfrage an die REST-APIJava

Java-Forum
Anonymous
 Spring Boot: 400 Bad Request beim Senden einer JSON-POST-Anfrage an die REST-API

Post by Anonymous »

Ich arbeite an einer Spring Boot-Anwendung, in der ich versuche, eine Question-Entität mithilfe einer POST-API zu speichern. Wenn ich jedoch eine JSON-Anfrage von Postman sende, erhalte ich 400 Bad Request.

Ich habe erwartet, dass die Anfrage erfolgreich verarbeitet wird und die Daten in der Datenbank gespeichert werden.

Controller

Code: Select all

@RestController
@RequestMapping("/api/questions")
public class QuestionController {

@Autowired
private QuestionService questionService;

@PostMapping
public ResponseEntity createQuestion(@RequestBody Question question) {
return ResponseEntity.ok(questionService.save(question));
}
}
Entität

Code: Select all

@Entity
public class Question {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String title;
private String content;

// getters and setters
}
JSON-Anfrage (Postman)

Code: Select all

{
"title": "How to handle exceptions in Spring Boot?",
"content": "I want to know the best practices for exception handling."
}
Fehler

Code: Select all

400 Bad Request
Required request body is missing
Was ich versucht habe
  • Sichergestellt, dass @RequestBody vorhanden ist
  • Verifiziertes JSON-Format
  • Überprüfter Inhaltstyp: application/json
  • Anwendung neu gestartet
  • Protokollierung aktiviert
Das Problem besteht jedoch weiterhin.

Umgebung
  • Spring Boot: 3.x
  • Java: 17
  • Betriebssystem: Ubuntu
  • Postman neueste Version

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post