SpringBoot MongoDB 返回 "Id must be assignable to Serializable! Object of class [null] must be an instance of interface java.io.Serializable"

标签 spring spring-boot spring-mongodb

我今天尝试向我的 API/DB 添加一个新集合,当我尝试 POST 或 GET 时,我收到此 500 错误响应:

{
  "cause": null,
  "message": "Id must be assignable to Serializable! Object of class [null] must be an instance of interface java.io.Serializable"
}

但是 POST 实际上是成功的,我可以在数据库中看到新数据。

型号:

@Setter
@Getter
public class League {

    private String name;
    private String shortName;
    private List<Team> teams;
}

存储库:

@RepositoryRestResource(collectionResourceRel = "leagues", path = "leagues", excerptProjection = LeagueProjection.class)
public interface LeagueRepository extends MongoRepository<League, String> {

}

投影:

@Projection(name="LeagueProjection", types={League.class})
public interface LeagueProjection {

    String getName();
    String getShortName();
}

我没有做任何特别的事情。我还有多个其他工作正常的集合。

我使用的是 spring-boot 1.5.1。

谢谢!

最佳答案

添加字段:

@Id private String id;

对于我的模型来说似乎已经解决了这个问题。

关于SpringBoot MongoDB 返回 "Id must be assignable to Serializable! Object of class [null] must be an instance of interface java.io.Serializable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42604007/

相关文章:

java - 认证后转到特定的jsp页面(spring security)

java - Non-resolvable import POM Failure to find

spring-boot - 我们可以为用户组/不同用户配置 Keycloak 多密码策略到单个领域吗?

java - 如何将聚合结果映射到java pojo

java - 在 Spring Security 中添加新用户

java - 尽管数据在数据库中,查询未返回结果

java - Spring Boot 测试数据库初始化运行两次

java - 如何将 Flux 从 Mono 内部的列表中取出?

spring - 我们可以在 mongorepository 中使用带空格的 Ignorecase 吗?

spring - Spring JavaMailSender:使其异步且持久