Jersey API返回ArrayList,其中包含没有子类属性的对象

时间:2017-05-10 11:21:44

标签: java json arraylist subclass

使用我的API,我必须返回Object(fs.file-max = 1000000),其中包含一个带有Objects(Playlist)的ArrayList。

Playlist.java

Track

但是,import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import java.util.ArrayList; import java.util.Arrays; @XmlRootElement public class Playlist { private String owner; private String name; private ArrayList<Track> tracks; public Playlist() { this.tracks = new ArrayList<Track>(); } public Playlist(String owner, String name, Track[] tracks) { this.owner = owner; this.name = name; this.tracks = new ArrayList<Track>(Arrays.asList(tracks)); } public void addTrack(Track track) { this.tracks.add(track); } @XmlElement(name = "tracks") public ArrayList<Track> getTracks() { return this.tracks; } public void changeName(String name) { this.name = name; } public String getOwner() { return owner; } public void setOwner(String owner) { this.owner = owner; } public String getName() { return name; } public void setName(String name) { this.name = name; } } 是使用TrackSong的实际类的超类。 在Video中,只找到ArrayListSong Objects,其中包含一些单独的属性。
例如,Video ObjectsSong的一部分而String:albumVideo

API返回的JSON仅显示String:description的共享属性,但不显示Track album所属的共享属性或Song给出的description }。

我该如何解决这个问题?

活动

Video

Song.java

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.GregorianCalendar;

@Path("/api/playlist")
public class PlaylistActivityREST {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Playlist getPlaylist() {
        Playlist playlist = new Playlist(
                "Owner",
                "Playlist name",
                new Track[]{
                        new Song(
                                "Ed Sheeran",
                                "Galway Girl",
                                "https://www.youtube.com/watch?v=87gWaABqGYs",
                                ((Integer) (60 * 3 + 19)).longValue(),
                                "÷"
                        ),
                        new Video(
                                "Ed Sheeran",
                                "Shape of You",
                                "https://www.youtube.com/watch?v=JGwWNGJdvx8",
                                ((Integer) (60 * 4 + 24)).longValue(),
                                25177705,
                                new GregorianCalendar(2017, 5, 3),
                                "÷. Out Now: https://atlanti.cr/yt-album"
                        )
                }
        );

        return playlist;
    }
}

Video.java

public class Song extends Track {

    private String album;

    public Song(String performer, String title, String url, long duration, String album) {
        super(performer, title, url, duration);
        this.album = album;
    }

    public String getAlbum() {
        return album;
    }

    public void setAlbum(String album) {
        this.album = album;
    }
}

Track.java

public class Video extends Track {
    private int playCount;
    private Calendar publicationDate;
    private String description;

    public Video(String performer, String title, String url, long duration, int playCount, Calendar publicationDate, String description) {
        super(performer, title, url, duration);
        this.playCount = playCount;
        this.publicationDate = publicationDate;
        this.description = description;
    }

    public int getPlayCount() {
        return playCount;
    }

    public String getPlayCountThousandsSeperator() {
        return String.format(Locale.US, "%,d", this.playCount).replace(',', '.');
    }

    public void setPlayCount(int playCount) {
        this.playCount = playCount;
    }

    public Calendar getPublicationDate() {
        return publicationDate;
    }

    public void setPublicationDate(Calendar publicationDate) {
        this.publicationDate = publicationDate;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

1 个答案:

答案 0 :(得分:0)

经过更长时间的研究,我找到了this answer,它也可以解决这个问题。

现在以

开头
@XmlRootElement
@XmlSeeAlso({Song.class,Video.class}) //This fixed it
public class Playlist { ... } // Omitting unchanged code