You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot+Hibernate向MySQL插入空值问题求助

Fixing Null Value Insertion with POST Request in Spring Boot + Hibernate

Hey, let's figure out why your POST request is inserting null values even though you're sending a valid JSON payload. I've dealt with this exact headache before, so here are the most likely culprits and fixes to check step by step:

1. Missing @RequestBody Annotation in Your Controller

First up, double-check your controller method for the /shipwrecks/add endpoint. If you don't annotate the request object with @RequestBody, Spring has no idea to map the incoming JSON payload to your entity class. That means all fields will be null when you pass it to the service layer.

Here's what the correct method should look like:

@PostMapping("/shipwrecks/add")
public ResponseEntity<Shipwreck> addShipwreck(@RequestBody Shipwreck shipwreck) {
    Shipwreck savedShipwreck = userService.save(shipwreck); // Adjust service reference if needed
    return ResponseEntity.ok(savedShipwreck);
}

(Side note: You mentioned UserController but your endpoint is for shipwrecks—make sure you're looking at the right controller class!)

2. Entity Class Issues (No Getters/Setters or Mapping Annotations)

Hibernate might have created the user table, but if your entity class (looks like it's Shipwreck based on the SQL query) doesn't have proper accessor methods or JSON mapping annotations, Spring can't populate its fields from the request.

  • Add Getters/Setters: Either write them manually, or use Lombok's @Data annotation at the top of your entity class to auto-generate them. Without setters, Spring can't assign values to the fields.
  • Map JSON Fields Correctly: If your JSON payload uses different field names than your entity, use @JsonProperty to map them. For example:
    import com.fasterxml.jackson.annotation.JsonProperty;
    
    @Entity
    @Table(name = "shipwreck") // Match your actual table name here
    public class Shipwreck {
        @JsonProperty("wreck_name") // Use if your JSON uses "wreck_name" instead of "name"
        private String name;
        private String description;
        private Long id2;
    
        // Getters and Setters (or replace with @Data annotation)
    }
    
  • Check Hibernate Annotations: Make sure your entity has @Entity and @Table (if needed) annotations, and that fields are mapped correctly to database columns with @Column if names don't match.

3. Invalid Request Format or Headers

Even if your code is right, a misconfigured Postman request can cause null values:

  • Confirm Content-Type Header: Ensure you've set the request header Content-Type to application/json—not text/plain or anything else. Spring ignores JSON payloads if the header isn't set correctly.
  • Validate JSON Payload: Make sure your request body is valid JSON. For example:
    {
        "name": "Sunken Galleon",
        "description": "17th century merchant ship",
        "id2": 101
    }
    
    Typos like missing quotes, commas, or mismatched field names will break the mapping.

4. Primary Key Configuration (If id2 is Your PK)

If id2 is the primary key for your shipwreck table, check if it's set to auto-generate. If you're expecting Hibernate to handle it automatically but haven't added @GeneratedValue, you might be passing a null value for id2—though this wouldn't cause the other fields to be null. Still, it's worth verifying:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // Use your preferred generation strategy
private Long id2;

Start with the first two checks—they're the most common causes of this issue. Once you fix the mapping between the JSON payload and your entity, the null values should disappear.

内容的提问来源于stack exchange,提问作者bc291

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:18:21