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

POST请求提交数据全部存入username列的问题排查求助

问题:POST请求JSON数据无法正确映射到数据库字段

我使用Postman发送包含两个字符串的JSON格式POST请求,内容为:

{"username":"mm46676","mail":"mm46676@gmail.com"}

但所有数据都被保存到数据库的username列中,预期结果是mm46676存入username列,mm46676@gmail.com存入mail列,通过H2控制台查看数据库存储情况不符合预期。相关代码如下:

EmployeeController.java

import com.incompatibleTypes.plancation.plancationapp.model.Employee;
import com.incompatibleTypes.plancation.plancationapp.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping(path = "/employee")
public class EmployeeController {

    private final EmployeeRepository employeeRepository;

    public EmployeeController(EmployeeRepository employeeRepository) {
        this.employeeRepository = employeeRepository;
    }

    @GetMapping("/all")
    public @ResponseBody Iterable<Employee> showAllEmployees(){
        return employeeRepository.findAll();
    }

    @PostMapping(path = "/add")
    public @ResponseBody String addNewEmployee (@RequestBody String username, String mail){
        Employee employee = new Employee();
        employee.setUsername(username);
        employee.setMail(mail);
        employeeRepository.save(employee);
        return "Employee Saved!";
    }
}

Employee实体类

package com.incompatibleTypes.plancation.plancationapp.model;

import jakarta.persistence.*;
import lombok.Data;


import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

@Data
@Entity
@Table(name = "employee")
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID id;

    private Role role;
    @Column(name = "username")
    private String username;
    @Column(name = "mail")
    private String mail;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "department_id")
    private Department department;


    @ManyToOne
    @JoinColumn(name = "minimal_vacation_day_id") 
    private VacationDay minimalVacationDay;
    @ManyToMany
    @JoinTable(
            name = "employee_bonusvacdays",
            joinColumns = @JoinColumn(name = "bonus_vacation_day_id"),
            inverseJoinColumns = @JoinColumn(name = "employee_id")
    )
    private Set<BonusVacationDay> bonusVacationDays = new HashSet<>();
    private LocalDateTime dateEmployed;

}

解决方案

错误原因

addNewEmployee方法的参数绑定逻辑错误:

  • @RequestBody注解被单独加在String username参数上,这会让Spring把整个JSON请求体的完整内容解析成字符串赋值给username,而mail参数因为没有绑定请求体,会被Spring识别为URL请求参数(如/employee/add?mail=xxx),但你是通过请求体发送JSON,所以mail实际为null。最终导致整个JSON字符串存入username列,mail列无数据。

修正代码

直接将@RequestBody绑定到Employee实体类参数,Spring会自动将JSON请求体的字段映射到实体类对应的属性:

import com.incompatibleTypes.plancation.plancationapp.model.Employee;
import com.incompatibleTypes.plancation.plancationapp.repository.EmployeeRepository;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping(path = "/employee")
public class EmployeeController {

    private final EmployeeRepository employeeRepository;

    public EmployeeController(EmployeeRepository employeeRepository) {
        this.employeeRepository = employeeRepository;
    }

    @GetMapping("/all")
    public @ResponseBody Iterable<Employee> showAllEmployees(){
        return employeeRepository.findAll();
    }

    @PostMapping(path = "/add")
    public @ResponseBody String addNewEmployee (@RequestBody Employee employee){
        employeeRepository.save(employee);
        return "Employee Saved!";
    }
}

补充注意事项

  1. 由于你的Employee实体类使用了@Data注解(Lombok提供),自动生成了getter/setter方法,Spring可以正常完成JSON到实体类的反序列化。
  2. 确保Postman请求的Content-Type设置为application/json,否则Spring无法正确解析JSON请求体。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 04:22:07