我有课本:
@Entity
@Table(name = "books")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType
public class Book {
@Id
@XmlAttribute
private String isbn;
private String title;
private String authors;
private int year;
@OneToMany(mappedBy="reservedBook")
private List<Reservation> bookReservations;
//Getters, setters, addReservation, remove Reservation
......................
}
然后,我有课程预订
@Entity
@Table(name = "reservations")
@XmlRootElement
public class Reservation {
private String username;
private String isbn;
@Temporal(TemporalType.DATE)
private Date date;
@ManyToOne
@JoinColumn(name = "isbn")
private Book reservedBook;
@ManyToOne
@JoinColumn(name = "username")
private User userWhoReserved;
//Getters and setters
...........
}
在资源类中,我尝试获取这样的特定书籍:
@GET
@Path("/{isbn}")
@Produces(MediaType.TEXT_XML)
public Book getBookByIsbn(@PathParam("isbn") String isbn) {
Book book = entityManager.find(Book.class, isbn);
if (book != null) {
return book;
}
}
现在,它不会序列化字段 List bookReservations。我尝试了很多在互联网上找到的想法,比如用 @XmlElement 为列表注释 getter 或在其他地方使用其他注释(我现在不记得它们了),但没有任何效果。
编辑:这是响应的样子:
<book isbn="3333333333">
<title>Mere Christianity</title>
<authors>C. S. Lewis</authors>
<year>2000</year>
</book>
但我也想表明保留意见。
解决方案似乎是什么?谢谢!索林