【问题标题】:One to Many Json Reponse With Jackson is not Working与杰克逊的一对多 Json 响应不起作用
【发布时间】:2018-09-18 22:55:44
【问题描述】:

我在使用 JPA 和 RelationsShips One to Many with Jackson 和 Spring Rest 时遇到问题...我尝试找到多种解决方案,但任何方法都对我有用,但我不知道问题出在哪里。

例如,我有一个具有一对多/多对一关系的表 Team

我有两个存储库,一个用于团队,另一个用于玩家

Team  >>> has Many >> Player
Player >>> many to one >> Team

我的实体团队有以下内容

@Entity
@Table(name = "teams")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Team {

    private static final long serialVersionUID = 1L;

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

    private long teamId;

    private String abbreviation;

    private String team;

    private String simpleName;

    private String logo;

    @OneToMany(cascade = {CascadeType.ALL,CascadeType.PERSIST,CascadeType.MERGE}, mappedBy = "team")
    @Column(nullable = false)
    private List<Player> players;

    Theirs getters/setter , hashcodes and string similars.

另一方面是实体玩家

    @Entity
@Table(name = "player")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Player {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private long id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "teams_id", nullable=true)
    private Team team;

    private String name;

所以,我在存储库的控制器中有典型的 get 调用。

@RestController
@RequestMapping("/api/public/team")
public class TeamController {

    @Autowired
    private TeamRepository teamRepository;

    @Autowired
    private GenericMethods genericMethods;

    @GetMapping(value = "/{id}")
    public Team getPersona(@PathVariable("id") int id) {
        return teamRepository.findOne(genericMethods.toLong(id));
    }

和存储库

@Repository
public interface TeamRepository extends JpaRepository<Team, Long> {

}

现在,当我调用此端点时,我收到以下答案,我认为这是不正确的,我只需要一个包含玩家的列表

{  
   "id":2,
   "teamId":0,
   "abbreviation":null,
   "team":null,
   "simpleName":"Betis",
   "logo":null,
   "players":[  
      {  
         "id":1,
         "team":2,
         "category":{  
            "id":1,
            "nombre":"juvenil a",
            "language":null,
            "description":null,
            "league":[  

            ],
            "players":[  
               1,
               {  
                  "id":2,
                  "team":2,
                  "category":1,
                  "name":"hulio"
               }
            ]
         },
         "name":"pepe"
      },
      2
   ]
}

我需要访问玩家和团队的信息,所以我不能使用@JsonIgnoreProperties

有人可以帮忙解决这个问题吗?

【问题讨论】:

  • 如果只需要播放器列表,为什么不直接返回列表呢?因为来电者已经认识团队。

标签: java spring rest api


【解决方案1】:

根据您真正想要实现的目标,您可以尝试不同的选择。我不确定您是否正在使用(或打算使用)spring-data-rest。

1.专用存储库

如果没有自己的存储库,Spring data rest 将嵌入相关实体。尝试创建public interface PlayersRepository extends JpaRepository...

2。延迟加载

您为什么使用 FetchType.EAGER ?尝试不使用它。

3.预测

预测仅适用于列表,而不适用于单个实体(即不明确您的要求)。您可以从 Teams 集合中隐藏玩家,即使它是默认返回的,如下所示:

@Projection(name = "noPlayers", types = { Team.class })
public interface TeamWithoutPlayers {

    Long getId();
    long getTeamId();
    String getAbbreviation();
    String getTeam();
    String getSimpleName();
    String getLogo();
}

More info - Spring Data Rest Projections

4.在使用@JsonIgnoreTeam 实体中序列化期间忽略

@JsonIgnore
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "teams_id", nullable=true)
private Team team;

最后的想法

使用 spring-data-rest 您可以扩展 CrudRepository 而不是 JpaRepository 并直接通过存储库访问该项目。这样你就不需要编写控制器了。

【讨论】:

    猜你喜欢
    • 2017-06-06
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 2017-01-11
    • 2014-05-28
    相关资源
    最近更新 更多