Angular 5 + Spring Boot POST请求无法正常工作问题求助
Hey there! Let's figure out why your POST request isn't passing data to the Spring Boot Rest Controller—since GET requests work perfectly, the issue is almost certainly related to how data is structured, sent, or parsed between frontend and backend. Let's walk through the most likely fixes step by step.
1. Verify Angular's POST Request Setup
First, make sure your Angular frontend is sending data in the right format and with correct headers:
- Set the
Content-Typeheader: Spring Boot expects JSON payloads by default, so explicitly set this header toapplication/jsonin your request. - Ensure proper serialization: When using Angular's
HttpClient, you can either pass theDishobject directly (it auto-serializes to JSON) or useJSON.stringify()for clarity. - Example Angular POST method:
import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Observable } from 'rxjs'; import { catchError } from 'rxjs/operators'; // ... addDish(dish: Dish): Observable<Dish> { const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }; // Either pass the object directly... return this.http.post<Dish>('/api/dishes', dish, httpOptions) .pipe(catchError(this.handleError)); // ...or stringify it explicitly (both work) // return this.http.post<Dish>('/api/dishes', JSON.stringify(dish), httpOptions) // .pipe(catchError(this.handleError)); } - Check the request payload: Open your browser's DevTools (F12 → Network tab), find the POST request, and look at the Request Payload section. If it's empty, the problem is in how you're passing the
Dishobject in Angular.
2. Fix Spring Boot's Dish Entity Class
Spring needs specific setup to correctly deserialize JSON into your entity:
- Add a no-argument constructor: This is mandatory for Spring to instantiate the class during deserialization.
- Include getters and setters: Spring uses these to populate fields from the JSON payload.
- Match field names (or use
@JsonProperty): If your AngularDishhas fields likedishNamebut your Spring class usesdishname, add@JsonPropertyto map them correctly. - Example Spring
Dishclass:import com.fasterxml.jackson.annotation.JsonProperty; public class Dish { private Long id; @JsonProperty("dishName") // Use this if Angular uses camelCase and Spring uses snake_case, etc. private String dishName; private double price; // Required no-arg constructor public Dish() {} // Optional parameterized constructor public Dish(Long id, String dishName, double price) { this.id = id; this.dishName = dishName; this.price = price; } // Getters and Setters for all fields public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getDishName() { return dishName; } public void setDishName(String dishName) { this.dishName = dishName; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
3. Correct the Spring Rest Controller
The most common mistake here is forgetting the @RequestBody annotation:
- Add
@RequestBodyto the method parameter: This tells Spring to parse the request body (not URL parameters or form data) into theDishobject. - Ensure the request path matches: Make sure the
@PostMappingpath aligns with what Angular is sending (e.g.,/api/dishes). - Example Controller code:
import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/dishes") public class DishController { // Inject your DishService here // private final DishService dishService; // public DishController(DishService dishService) { // this.dishService = dishService; // } @PostMapping public ResponseEntity<Dish> createDish(@RequestBody Dish dish) { // Save the dish using your service // Dish savedDish = dishService.save(dish); return new ResponseEntity<>(dish, HttpStatus.CREATED); // Return the saved dish } } - Without
@RequestBody, Spring will try to bind data from request parameters, which doesn't work for POST payloads—this is a super common oversight!
4. Enable Spring Boot Debug Logs
If you still don't see errors but data isn't coming through, turn on debug logging to see what's happening under the hood:
- Add these lines to your
application.propertiesfile:logging.level.org.springframework.web=DEBUG logging.level.com.fasterxml.jackson=DEBUG - The logs will show details about request parsing, field binding, and any hidden deserialization issues that aren't throwing explicit errors.
Quick Recap of Most Likely Fixes
- Angular: Set
Content-Type: application/jsonand confirm the payload is populated. - Spring Entity: Add a no-arg constructor and matching getters/setters.
- Spring Controller: Don't forget the
@RequestBodyannotation on theDishparameter.
内容的提问来源于stack exchange,提问作者Джулиян Овчаров

