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.

Dani AI

Generated

clarified the goal is to produce citation-style text (BibTeX, RIS, plain text, etc.) from the same input. 's suggestion to keep data structured (JSON/XML) still applies for the internal flow; 's MIME/email comment was a red herring for this use case. A simple, maintainable pattern is: (1) merge the two sources into a single DTO per ISBN, (2) render that DTO with a small formatter or a template per output format, and (3) stream the result back with the appropriate headers.

Merge lists by ISBN into a single list of combined records (Java 8 example):

Map<String, BookSummary> summaryMap = summaries.stream()
    .collect(Collectors.toMap(BookSummary::getIsbn, Function.identity()));

List<CombinedBook> combined = details.stream()
    .map(d -> new CombinedBook(d, summaryMap.get(d.getIsbn())))
    .collect(Collectors.toList());

Render using a formatter abstraction so formats stay isolated and easy to add:

@FunctionalInterface
interface Formatter {
    void format(CombinedBook b, Writer w) throws IOException;
}

A simple plain-text formatter implementation writes fields in the desired lines; a BibTeX formatter wraps/escapes values per BibTeX rules. For small/custom formats a handwritten Formatter is easiest; for complex or evolving formats use a template engine (Mustache/FreeMarker) with templates stored in resources.

Stream the output from JAX-RS to avoid large in-memory strings:

StreamingOutput stream = out -> {
  try (Writer w = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8))) {
    for (CombinedBook b : combined) formatter.format(b, w);
  }
};

return Response.ok(stream)
  .type("text/x-bibtex") // or "text/plain"
  .header("Content-Disposition", "attachment; filename=\"books.bib\"")
  .build();

Notes: choose a Content-Type that clients expect (many services use text/x-bibtex or simply text/plain and rely on the .bib/.ris filename). Escape special characters (curly braces, percent signs, newlines) when producing BibTeX/RIS. The pattern above keeps format logic separate from data retrieval and scales well when new formats are added.

Recommended Answers

All 3 Replies

I left your post to stew since it's not clear you want to send EMAIL with some content. Frankly I have to guess that since you wrote MIME.

Anyhow I'd just hit https://www.tutorialspoint.com/javamail_api/javamail_api_send_inlineimage_in_email.htm since your code is in JAVA and you appear to want to send something graphic.

For plain text, well, that's well done so I didn't bother to look it up.

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.