1

I have composed JSON response as below in my java servlet, where JObject is the JSON object created

response.setContentType("application/json; charset=UTF-8");
PrintWriter printout = response.getWriter();
printout.print(JObject);
printout.flush();

But it got received as text/plain in the receiving side

[Server: Apache-Coyote/1.1, ETag: W/"XXXXXXXXXX", Last-Modified: Tue, 04 Jun 2013 10:42:31 GMT, Content-Type: text/plain, Content-Length: 2573, Date: Tue, 04 Jun 2013 10:44:01 GMT]

How to get the exact JSON response? If i compose the JSON response in same machine, im getting the JSON data. But if i compose the JSON response in another server, its returning back as text/plain.

And this is the JObject:

JSONObject JObject = new JSONObject(); 
JObject.put("Response", "1"); 
JObject.put("Message", "Client unauthorized"); 
Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
John
  • 2,035
  • 13
  • 35
  • 44

4 Answers4

6

I am not sure whether exactly what code you have in the servlet. But I have created a sample Servlet and it returned the Json output with the same above code.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("application/json; charset=UTF-8");
        PrintWriter printout = response.getWriter();

        JSONObject JObject = new JSONObject(); 
        JObject.put("Response", "1"); 
        JObject.put("Message", "Client unauthorized"); 

        printout.print(JObject);
        printout.flush();
            // Or
            // printout.write(JObject.toString()); 
    }

And I got {"Message":"Client unauthorized","Response":"1"} as output on the browser.

Here is the result snap shot:

enter image description here

Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
1

response.getWriter().write(jsonObj.toString()) is working for me.

Balint Bako
  • 2,500
  • 1
  • 14
  • 13
  • To my surprise, both printout.print() and printout.write() working in all browsers.. But if i run it in eclipse IDE as run as 'run on server', its reading it as text.. have anyone experienced this before? – John Jun 04 '13 at 12:04
  • I'm running it in Eclipse too and it is working. This behaviour is independent from the browser as it is on the server-side. At least the response headers and the response body should be the same... – Balint Bako Jun 04 '13 at 12:08
0

I use Gson

Gson gson = new Gson();
String jsonData = gson.toJson(student);//here student is object
PrintWriter out = response.getWriter();
try {
    out.println(jsonData);
} finally {
    out.close();
}
xrcwrn
  • 5,339
  • 17
  • 68
  • 129
0

If you're using Java 7's try-with-resources, make sure to set the

response.setContentType("application/json");

before your try-with-resources because if you set it afterwards, it'll close the PrintWriter beforehand and won't set any contentType. Then if it goes through an nginx server, it may set contentType to plain-text when it notices none set in the header, which was exactly my issue.

DO THIS:

response.setContentType("application/json");

try (PrintWriter out = response.getWriter()) {
    out.println(jsonData);
} 

NOT THIS:

try (PrintWriter out = response.getWriter()) {
    out.println(jsonData);
} 
// does not set contentType anymore because writer has been flushed already.
response.setContentType("application/json");
LethalLima
  • 211
  • 3
  • 6