0
使用我的API,我必須返回Object(Playlist
),其中有一個ArrayList,其中包含Objects(Track
)。Jersey API返回的ArrayList與沒有Subclass屬性的對象
Playlist.java
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;
}
}
然而,Track
是實際的類的超類使用Song
和Video
。
在ArrayList
中,只找到Song Objects
和Video Objects
,它們有幾個獨立的屬性。
例如,Song
是String:album
的一部分,並且Video
具有String:description
。
的JSON API返回只示出了從Track
共享屬性而不是album
一個Song
屬於或與Video
給出的description
。
我該如何解決這個問題?
活動
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;
}
}
Song.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;
}
}
Video.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;
}
}
Track.java
@XmlRootElement
public class Track {
private String performer;
private String title;
private String url;
private long duration;
public Track() {
}
public Track(String performer, String title, String url, long duration) {
this.performer = performer;
this.title = title;
this.url = url;
this.duration = duration;
}
public String getPerformer() {
return performer;
}
public void setPerformer(String performer) {
this.performer = performer;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public long getDuration() {
return duration;
}
public void setDuration(long duration) {
this.duration = duration;
}
public String getParsedDuration() {
return this.getDuration()/60 + ":" + this.getDuration() % 60;
}
}
嗨,你可以分享你的歌聲/視頻類?它看起來怎樣?從代碼看,你正在構造函數中傳遞所需的東西,但仍然...如果你能! –
已更新問題,添加了歌曲,視頻和跟蹤課程 – CreasolDev