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.

Hi,
If I get it right, your problem is on the front end : how to format the display ?

First, the most used format are XML and JSON because they allow us to use in-built parsers , so you won't be stuck in string processing.
To create a JSON response, you can check this example :
https://www.youtube.com/watch?v=zESNqWcY0pY
Adam Bien is an amazing guy who works with J2EE and Micro web services and here he gives a simple way to build a JSON response. You can see that yu can use "builders" to build the response so there is no need to generate the manually the JSON code.

Second, on the front end, I don't know a largely used API to do so, there is some small APIs like : https://plugins.jquery.com/jsontotable/
But you can easily use built-in parser to create Javascript objects from Json.

Good Luck.

Hey guys,

Sorry my question was not very clear. I'm building a web resource that spits out data in different formats like BibTex(.bib), RIS etc. for the same input. So in my example above hitting http://localhost/getLatexDetails?isbn=123456789&isbn=987654321 would result in a file with the content:

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

And hitting /getPlainText would result in a file with the content:

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

I was wondering if there is any easy way to build such responses i.e. the formatting in the required ways. Sorry for all the confusion. Not dealing with emails(sorry for using MIME in title - totally my ignorance) or front end. As you guys would probably know by now I'm a noob. Thanks for all the useful advice given. Any help on the formatting would be helpful. Once again thanks - I'm grateful guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.