简体   繁体   中英

xml not mapping to java pojo

I am trying to post xml to spring rest controller which has the below classes. XML is mapping correctly for Team object but its not mapping playerName in the Player object. playerName is always null. Can anyone please let me know what is wrong here.

My Player 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

@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

@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.

<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. below is the rest code for create player and player xml. it looks only mapping problem is child xml pojo in the parent 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

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

Attaching debug screen shot in which i can see playerName is null

在此处输入图片说明

You need to define XmlElement on the Players list.

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

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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