This might be a simple problem but I had difficulties finding a solution. I'm trying to return data in specified formats for the same input. I've data in two tables - Book_Details and Book_Summaries - which I need to combine in fixed formats. My requirement is that I need to be able to format data for multiple entries in a single entry. E.g. I request for detials of two books and the response looks like:
[{
"isbn": "123456789",
"author": "aut_last, aut_first",
"name": "book1 name",
"pages": 200,
"publisher": "penguin"
}, {
"isbn": "987654321",
"author": "aut_last, aut_first",
"name": "book2 name",
"pages": 300,
"publisher": "penguin"
}]
I capture this response in the following class:
public class BookDetail{
private String isbn;
private String author;
private String name;
private int pages;
private String publisher;
BookDetail(){}
public String getIsbn(){
return isbn;
}
public String getAuthor(){
return author;
}
public String getName(){
return name;
}
public int getPages(){
return pages;
}
public String getPublisher(){
return publisher;
}
}
So I would have a List of BookDetail for multiple book entries. Similarly there is a BookSummary for capturing this data:
[{
"isbn": "123456789",
"summary": "this is the summary of book1"
}, {
"isbn": "987654321",
"summary": "this is the summary of book2"
}]
I need to combine and format the data and return the response. I've entered two of the many formats required below:
/**
bknm: book1 name isbn: 123456789 autf: aut_first autl: aut_last page:200 publ:penguin summ: this is the summary of book1
bknm: book2 name isbn: 987654321 autf: aut_first autl: aut_last page:300 publ:penguin summ: this is the summary of book2
*/
@GET
@Path("/getLatexDetails")
@Produces("application/x-latex")
public Response getCompleteDetails(){
final BookDetail details = getBookDetails();
final BookSummary summaries = getBookSummaries();
// Transform to above desired format
return Response.ok(response).build();
}
/**
bk: book1 name
is: 123456789
af: aut_first
al: aut_last
pg: 200
pb: penguin
sm: this is summary of book1
bk: book2 name
is: 987654321
af: aut_first
al: aut_last
pg: 300
pb: penguin
sm: this is summary of book2
*/
@GET
@Path("/getTextDetails")
@Produces("text/plain")
public Response getCompleteDetails(){
final BookDetail details = getBookDetails();
final BookSummary summaries = getBookSummaries();
// Transform to the above desired format
return Response.ok(response).build();
}
In both cases there will be mutiple book entries and they will be separated by a line break. What would be the best way to format them? Anything like templates? Any help would be appreciated.