1

The question can seem simple, but I didn't find a good answer yet. I need to send a JSon structure (build with an unspecified libretry I'm currently developing) from a Servlet to a remote page.

I'm interested in the best way to send the structure.

I mean, in my Servlet, inside the doPost() event, how should I manage the send?

I was thinking about 2 scenarios:

try (PrintWriter out = response.getWriter()) {
   out.print(myJSon.toString(); // <- recursive function that overrides 
                                // toString() and returns the entire JSon 
                                // structure

} (...)

or

try (OutputStream os = response.getOutputStream()) {
   myJSon.write(os, StandardCharsets.UTF8); // <- function that 
// recursively writes chunk of my JSon structure 
// in a BufferWriter created inside the root write function
// forcing UTF-8 encoding

} (...)

Or something different, if there's a better approch.

Note that the JSon structure contains an array of objects with long text fields (descriptions with more than 1000 characterd), so it can be quite memory consuming.

For why I'm not using standard JSon libreries, it's because I don't know them and I don't know if I can trust them yet. And also I don't know if I will be able to install them on the production server.

Thanks for your answers.

Luca Scarcia
  • 208
  • 1
  • 11

2 Answers2

2

From your question i see multiple points to adress:

  1. How to send your JSon
  2. What JSon library can you use
  3. How to use the library in production

How to send your JSon

From your code this seems to be an HTTP response rather than a POST on your Servlet so you need to know how to send a JSON string as an HTTP response's body

Do you use a framework for your web server or are you handling everything manually ? If you use a framework it usually does it for you, just pass the JSON String

If your doing it manually:

try (PrintWriter pw = response.getWriter()) {
   pw.write(myJson.toString());
}

or

try (OutputStream os = response.getOutputStream()) {
   os.write(myJson.toString().getBytes());
}

Both are valid, see Writer or OutputStream?

Your JSON's size shouldn't matter given what your saying, it's just text so it won't be big enough to matter.


What libraries can you use

There are a lot of JSON libraries for Java, mainly:

Go for the one you prefer, there will be extensive documentation and resources all over google


How to use in production

If you are not sure you are able to install dependencies on the production server, you can always create an uber-jar (See @Premraj' answer)

Basically, you bundle the dependency in your Jar

Czoo
  • 394
  • 1
  • 6
  • 20
  • doPost is the method that andles the client request in my servlet. I receive a request and respond with a JSon String. My worry is that using toString, I'm generating a lot of garbage chunk of memory fragments while building this string. The string itself can reach about 64k. Count 1000 similar requests per minute, it can weight quite a lot. That's why I was thinking about letting every single object in my structure write directly into a BufferedWriter – Luca Scarcia Dec 12 '17 at 17:01
  • Are you running on a memory constrained environment ? If so StringBuilder is indeed the best implementation for you, otherwise existing libraries will be optimized enough. I didn't quite get the "letting every object write directly" part, you mean that you write each object one by one instead of adding them to a JSON list to send back ? – Czoo Dec 14 '17 at 14:31
  • Perhaps I'm too old as a programmer, so I try to avoid abuse of memory if I can. Therefore, if I have some strings and want to send them through a stream, i write them one by one instead of creating a single huge string and send them all at once. – Luca Scarcia Dec 14 '17 at 16:52
  • Yes i see what you mean, there's no reason for it not to work, you could do some tests to observe the memory overhead between your method and converting the whole to one JSon string beforre sendiing – Czoo Dec 15 '17 at 09:41
  • 1
    @LucaScarcia did it help you ? If so could you validate my answer ? Thx – Czoo Apr 13 '18 at 15:24
  • What I did in the end is create a recursive function `public void write(Writer pw) throws IOException` that recursively calls every node in my JSon structure and writes it to the stream. The question was more about, creating a big string first or writing recursively every chunk of data into the stream. I'm sorry to say your answer wasn't really relate to the question (the point wasn't really using OutputStream or PrintWriter). Well, your answer was enlightning on other aspects, but still it realli didn't answer my question, sorry. – Luca Scarcia Apr 16 '18 at 14:58
0

Using Gson is good way to send json

Gson gson = new Gson();
String jsonData = gson.toJson(student);
PrintWriter out = response.getWriter();
try {
    out.println(jsonData);
} finally {
    out.close();
}

for detail json response from servlet in java

xrcwrn
  • 5,339
  • 17
  • 68
  • 129