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

Spring Boot中@RequestBody接收Postman JSON数据时字段全为null

问题描述

我尝试通过Spring Boot的@RequestBody接收参数,修改数据库中通过id查询到的已有User对象。数据库中的对象能正常找到,但接收的User对象所有String类型字段都被解析为null,打印接收的数据时这些字段全是null,最终导致数据库里的对应字段被更新为null。我和别人核对了Model、Controller、Service代码,完全一致,但对方的代码可以正常运行,我的却不行。

相关代码

Model类

@Entity
@Table(name="user")
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
@Getter
@Setter
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id", nullable = false, updatable = false)
    private Long id;

    @Column
    private String name;

    @Column
    private String email;

    @Column
    private String password;

    @Column
    private String role;

    @Column
    private double somme_dispo;
}

Controller方法

import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fan2foot.fan2foot.repository.UserRepository;
import com.fan2foot.fan2foot.service.UserService;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import com.fan2foot.fan2foot.model.User;

@PutMapping("/update/{id}")
public ResponseEntity<User> update(@PathVariable Long id, @RequestBody User user)  {
    System.out.println(user.getName());
    System.out.println(user.getEmail());
    System.out.println(user.getPassword());
    System.out.println(user.getRole());
    User users = userService.updateUser(id, user);
    System.out.println(users.getName());
    System.out.println(users.getEmail());
    System.out.println(users.getPassword());
    System.out.println(users.getRole());
    return new ResponseEntity<>(users, HttpStatus.OK);
}

Service方法

public User updateUser(Long id, User user) {
    return userRepository.findById(id)
            .map(u -> {
                u.setName(user.getName());
                u.setEmail(user.getEmail());
                u.setPassword(user.getPassword());
                u.setRole(user.getRole());
                u.setSomme_dispo(user.getSomme_dispo());
                return userRepository.save(u);
            }).orElseThrow(() -> new RuntimeException("User non trouvé..."));
}
排查解决方案
  1. 替换@RequestBody的导入包
    你的Controller中导入的是Swagger的io.swagger.v3.oas.annotations.parameters.RequestBody,这不是Spring用来解析请求体的注解。必须换成Spring官方的org.springframework.web.bind.annotation.RequestBody,这是导致字段解析为null的核心原因。

  2. 校验请求格式
    确保请求时设置了正确的请求头Content-Type: application/json,同时请求体的JSON字段名和User类的属性名完全匹配(大小写敏感)。

  3. 检查依赖完整性
    确认项目已引入spring-boot-starter-web依赖,该依赖包含了Spring MVC和Jackson JSON解析组件,缺失会导致请求体无法正常解析。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 06:42:59