使用我的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;
}
}
是使用Track
和Song
的实际类的超类。
在Video
中,只找到ArrayList
和Song Objects
,其中包含一些单独的属性。
例如,Video Objects
是Song
的一部分而String:album
有Video
。
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;
}
}
答案 0 :(得分:0)
经过更长时间的研究,我找到了this answer,它也可以解决这个问题。
现在以
开头@XmlRootElement
@XmlSeeAlso({Song.class,Video.class}) //This fixed it
public class Playlist { ... } // Omitting unchanged code