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

Entity类引发415 Unsupported Media Type错误排查求助

415 Unsupported Media Type 错误排查:User实体类相关问题

发送POST请求创建新用户时收到415 Unsupported Media Type错误。已排查Content-Type请求头、项目依赖及配置,且通过自定义测试类验证代码逻辑正常,锁定问题出在User实体类,但无法确定具体原因。

Rest控制器代码

@RestController
@RequestMapping("/api/users")
@CrossOrigin(origins = "http://localhost:3000/", maxAge = 3600)
public class UserRestController {
    ...

    @PostMapping()
    public ResponseEntity<UserDto> create(@RequestBody User user) {
        User result = userService.create(user);
        return new ResponseEntity<>(UserDto.fromUser(result), HttpStatus.OK);
    }
}

User实体类代码

@Entity
@Table(name = "users")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User extends BaseEntity {

    @Column(name = "username")
    private String username;

    @Column(name = "password")
    private String password;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Enumerated(EnumType.STRING)
    @Column(name = "role")
    private Role role;

    @Column(name = "active")
    private boolean active;

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonManagedReference
    private List<Restaurant> restaurants;

    @OneToMany(mappedBy = "author", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonManagedReference
    private List<Comment> comments;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "vote_id")
    @JsonBackReference
    private Vote vote;

    @Override
    public String toString() {
        return "User{" +
                "id='" + getId() + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", role=" + role +
                ", active=" + active +
                '}';
    }
}

BaseEntity类代码

@MappedSuperclass
@Data
public class BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;

    @CreatedDate
    @Column(name = "created")
    private LocalDateTime created;

    @LastModifiedDate
    @Column(name = "updated")
    private LocalDateTime updated;

}

Postman报错截图

Postman请求头截图
Postman响应截图

可能的问题点及解决建议

1. LocalDateTime序列化/反序列化配置缺失

BaseEntity中的created和updated字段为LocalDateTime类型,若Jackson未正确配置Java 8日期类型支持,会导致反序列化失败触发415错误。

  • 解决:确保项目依赖包含jackson-datatype-jsr310,并在配置类中注册该模块,或在字段上添加@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")注解。

2. 全参构造器导致的反序列化冲突

@AllArgsConstructor生成的构造器包含所有字段(包括关联集合和Vote对象),而请求体中不会传入这些关联字段,Jackson尝试匹配构造器时会失败。

  • 解决:移除@AllArgsConstructor,或添加仅包含基础字段(username、password、firstName、lastName、role、active)的构造器,并使用@JsonCreator标记该构造器。

3. 双向关联的JSON序列化问题

虽然使用了@JsonManagedReference和@JsonBackReference处理循环引用,但关联实体(Restaurant、Comment、Vote)若未正确配置,或请求体意外包含关联字段,会导致解析失败。

  • 解决:临时注释掉restaurants、comments、vote字段重新测试,若请求正常,再逐个排查关联实体的序列化配置;同时确保请求体中不包含关联对象的字段。

4. 枚举类型Role的解析错误

请求体中传入的role值需与Role枚举中的字符串值完全一致(大小写敏感),否则会触发反序列化失败。

  • 解决:检查请求体中的role字段值,确保与枚举定义匹配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 03:40:16