简体   繁体   English

GSON的序列化对象未正确结束

[英]Serialized object with GSON not properly ended

I have a struts Action, that serves JSON data. 我有一个struts Action,它提供JSON数据。
This data is serialized from a POJO with basic elements, using GSON. 使用GSON从具有基本元素的POJO序列化此数据。

In localhost , with Tomcat 6, system works correct, but when I deploy the app to Weblogic 9, the result from servlet, is not OK, the resulting JSON is not properly ended (it misses a }). 在Tomcat 6的localhost ,系统可以正常运行,但是当我将应用程序部署到Weblogic 9时,来自servlet的结果就不好了,生成的JSON没有正确结束(错过了})。

This is the pojo (I omited getters and setters) 这就是pojo(我省略了吸气剂和吸气剂)

public class DMTestResponse
{
    private String codiError;
    private String descripcioError;
    private Dades dades = new Dades();

    public class Dades
    {
            private String dada1;
            private String dada2;
            private Integer dada3;
            private String dada4;
    }
}

And this is the part of code that serializes the object: 这是序列化对象的代码部分:

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException
{
    // Some things
    response.setContentType("application/json");
    ServletOutputStream os = response.getOutputStream();
    os.write(resp.toString().getBytes(), 0, resp.length());
    os.flush();
    os.close();
}

From tomcat, the respose is: 在tomcat中,解决方法是:

{"codiError": "0", "dades": {"dada1":"bla", "dada2":"bla","dada3":"50";"dada4":"text llarg suspensió"}}

But from weblogic, the respose is: 但是从weblogic来看,解决方法是:

{"codiError": "0", "dades": {"dada1":"bla", "dada2":"bla","dada3":"50";"dada4":"text llarg suspensió"}

How you can see, in weblogic, the last '}' is missing. 在weblogic中,您怎么看不到最后的'}'。

What may be the cause ? 可能是什么原因?

Different servers could be configured to for different encoding. 可以将不同的服务器配置为使用不同的编码。 Try 尝试

response.setContentType("application/json; charset=UTF-8");
ServletOutputStream os = response.getOutputStream();
String s = resp.toString();
byte[] ba = s.getBytes("UTF-8");
os.write(ba, 0, ba.length);
os.flush();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM