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));
}
}
Code: Select all
@Entity
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String content;
// getters and setters
}
Code: Select all
{
"title": "How to handle exceptions in Spring Boot?",
"content": "I want to know the best practices for exception handling."
}
Code: Select all
400 Bad Request
Required request body is missing
- Sichergestellt, dass @RequestBody vorhanden ist
- Verifiziertes JSON-Format
- Überprüfter Inhaltstyp: application/json
- Anwendung neu gestartet
- Protokollierung aktiviert
Umgebung
- Spring Boot: 3.x
- Java: 17
- Betriebssystem: Ubuntu
- Postman neueste Version
Mobile version