Im very new to Jersey REST and I'm trying to build a sample library system. I have created my BookResource.java and I want it to display the following output in browser:
{
“book” : {
“isbn” : 1,
“title” : “Programming Amazon EC2”,
“publication-date” : “2/11/2011”,
“language” : “eng”,
“num-pages”: 185,
“status” : “available”,
“authors” : [
{ “rel”: “view-author”, “href”: “/books/1/authors/1”, “method”: “GET” },
{ “rel”: “view-author”, “href”: “/books/1/authors/2”, “method”: “GET” }
]
},
"links" : []
}
My Book.java is below:
public class Book
{
private long isbn;
private String title;
@JsonProperty("publication-date")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM/dd/yyyy")
private String date;
private String language;
private String status;
@JsonProperty("num-pages")
private Integer num_pages;
@JsonProperty("authors")
private ArrayList<Author> authorList;
public Book() {}
//getters and setters
}
Author.java
public class Author
{
@JsonProperty("name")
private String name;
public Author() {}
public String getAuthor() {
return name;
}
public void setAuthor(String newAuthor){
this.name = newAuthor;
}
}
BookRepository.java
public class BookRepository implements BookRepositoryInterface {
public ArrayList<Author> getAuthorByIsbn(Long isbn)
{
Book newBook = bookInMemoryMap.get(isbn);
ArrayList<Author> authorList = newBook.getAuthor();
return authorList;
}
}
However my actual GET response on browser is showing as:
{
"book": {
"isbn": 1,
"title": "Programming EC2r",
"language": "eng",
"status": "available",
"author": [{
"name": "auth1",
"author": "auth1"
}, {
"name": "auth2",
"author": "auth2"
}],
"num_Pages": 185,
"publication-date": "2/11/2011",
"num-pages": 185,
"authors": [{
"name": "auth1",
"author": "auth1"
}, {
"name": "auth2",
"author": "auth2"
}]
}
BookResource.java
public class BookResource
{
@GET
@Path("/{isbn}")
@Timed(name = "view-book")
public BookDto getBookByIsbn(@PathParam("isbn") LongParam isbn)
{
Book book = bookRepository.getBookByISBN(isbn.get());
BookDto bookResponse = new BookDto(book);
// add more links
bookResponse.addLink(new LinkDto("view-book", "/books/" + book.getIsbn(),"GET"));
return bookResponse;
}
}
I am getting multiple values for author and num_pages and cannot seem to get the output displayed correctly. I have tried @JsonPropertyOrder, but it didn't work. Also, in debug I checked that the Book resource is getting stored and retrieved correctly. It's only the JSON output which is displaying multiple values. How can I correct that? Also, how can I display the links for "authors" as required ?