java JSON 响应以文本形式返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16915916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:23:21  来源:igfitidea点击:

JSON response is returning as text

javajsonservletshttpresponse

提问by John

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

我在我的 java servlet 中编写了如下的 JSON 响应,其中 JObject 是创建的 JSON 对象

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 JSONresponse in same machine, im getting the JSON data. But if i compose the JSONresponse in another server, its returning back as text/plain.

如何获得准确的 JSON 响应?如果我JSON在同一台机器上撰写响应,我将获得 JSON 数据。但是,如果我JSON在另一台服务器中撰写响应,它会返回为text/plain.

And this is the JObject:

这是JObject

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

回答by Hardik Mishra

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.

我不确定您在 servlet 中到底有什么代码。但是我创建了一个示例 Servlet,它返回了与上述代码相同的 Json 输出。

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.

{"Message":"Client unauthorized","Response":"1"}在浏览器上得到了输出。

Here is the result snap shot:

这是结果快照:

enter image description here

在此处输入图片说明

回答by Balint Bako

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

response.getWriter().write(jsonObj.toString())正在为我工​​作。

回答by xrcwrn

I use Gson

我使用 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();
}

回答by LethalLima

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

如果您使用的是 Java 7 的 try-with-resources,请确保设置

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.

在您尝试使用资源之前,因为如果您在之后设置它,它将事先关闭 PrintWriter 并且不会设置任何内容类型。然后,如果它通过 nginx 服务器,当它注意到标题中没有设置时,它可能会将 contentType 设置为纯文本,这正是我的问题。

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");