2016-05-11 84 views
2

我想发布xml到弹簧休息控制器,它具有下面的类。 XML正在为Team对象正确映射,但它没有在Player对象中映射playerName。 playerName始终为空。任何人都可以让我知道这里有什么问题。xml没有映射到java pojo

我的球员XML POJO

@XmlRootElement 
@Entity(name = "player") 
public class Player { 

    @Id 
    @GeneratedValue 
    @Column(name = "player_id") 
    private long player_id; 

    @Column(unique=true , nullable = false) 
    private String playerName; 

    @ManyToOne 
    @JoinColumn(name = "team_id") 
    private Team team; 

    public long getPlayer_id() { 
     return player_id; 
    } 

    public void setPlayer_id(long player_id) { 
     this.player_id = player_id; 
    } 


    public String getPlayerName() { 
     return playerName; 
    } 

    public void setPlayerName(String playerName) { 
     this.playerName = playerName; 
    } 

    public Team getTeam() { 
     return team; 
    } 

    public void setTeam(Team team) { 
     this.team = team; 
    } 


} 

我的团队XML POJO

@XmlRootElement 
@Entity(name = "team") 
public class Team { 

    @Id 
    @GeneratedValue 
    @Column(name = "team_id") 
    private long team_id; 

    @Column(unique=true , nullable = false) 
    private String name; 

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "team") 
    private List<Player> players; 

    public long getTeam_id() { 
     return team_id; 
    } 

    public void setTeam_id(long team_id) { 
     this.team_id = team_id; 
    } 


    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public List<Player> getPlayers() { 
     return players; 
    } 

    public void setPlayers(List<Player> players) { 
     this.players = players; 
    } 


} 

我的春天休息控制器方法

@RequestMapping(value="/team/", method = RequestMethod.POST , headers="Accept=application/xml") 
    @ResponseStatus(value = HttpStatus.OK) 
    public void createTeam(@RequestBody Team team) throws Exception { 
     for (Player player: team.getPlayers()){ 
      player.setTeam(team); 
     } 
     teamService.createTeam(team); 
    } 

我的XML对此我张贴到控制器。

<team> 
<name>GUj</name> 
<players> 
    <Player> 
    <playerName>Raina</playerName> 
    </Player> 
</players> 
</team> 

当我直接调用与播放器的XML,然后它的工作球员休息方法。下面是创建播放器和播放器xml的其余代码。它看起来只是映射问题,是父xml pojo中的子xml pojo。

@RequestMapping(value="/player/", method = RequestMethod.POST , headers="Accept=application/xml") 
    @ResponseStatus(value = HttpStatus.OK) 
    public void createPlayer(@RequestBody Player player) throws Exception { 
     playerService.createPlayer(player); 
    } 

球员XML

<player> 
<playerName>Yuvraj</playerName> 
</player> 

附加调试屏幕截图中,我可以看到playerName为空

enter image description here

+0

不应该标签为? – Jagrut

+0

你试过配置jacksonObjectMapper吗?这照顾了映射。 –

+0

@Jagrut我试过这种情况,但没有区别。 – OneTwo

回答

0

您需要的球员名单上的定义XmlElement

@XmlElement(name="players") 
public List<Player> getPlayers() { 
    return players; 
} 

希望这有助于!

+0

Jagrut - 我试过这个,仍然问题没有解决。 – OneTwo