简体   繁体   English

xml不映射到java pojo

[英]xml not mapping to java pojo

I am trying to post xml to spring rest controller which has the below classes. 我正在尝试将xml发布到具有以下类的spring rest控制器。 XML is mapping correctly for Team object but its not mapping playerName in the Player object. XML为Team对象正确映射,但没有映射Player对象中的playerName。 playerName is always null. playerName始终为null。 Can anyone please let me know what is wrong here. 任何人都可以让我知道这里出了什么问题。

My Player XML 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;
    }


}

My Team XML POJO 我的团队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;
    }


}

My Spring Rest Controller method 我的Spring Rest Controller方法

@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);
    }

My XML which i am posting to controller. 我要发布到控制器的XML。

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

When i directly call player rest method with player xml then its working. 当我直接使用播放器xml调用播放器rest方法时,其工作原理。 below is the rest code for create player and player xml. 以下是用于创建播放器和播放器xml的其余代码。 it looks only mapping problem is child xml pojo in the parent xml pojo. 它看起来唯一的映射问题是父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);
    }

player xml 播放器XML

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

Attaching debug screen shot in which i can see playerName is null 附上我可以看到playerName为null的调试屏幕截图

在此处输入图片说明

You need to define XmlElement on the Players list. 您需要在“播放器”列表上定义XmlElement

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

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM