spring-mvc与jetty服务器上的resteasy字符编码问题

发布于 2024-11-03 05:34:39 字数 985 浏览 0 评论 0原文

我正在尝试在jetty服务器上实现restful协议。我有可运行的服务器,我可以从我的其余客户端访问它。我的服务器端项目是一个maven项目。我有一个关于字符编码的问题。当我在从控制器发送它之前检查响应时,不存在编码问题。但是在我向客户端返回响应后,我看到了损坏的数据。响应头是 UTF-8。另外,我有一个针对此问题的侦听器,并且我设置为请求和响应 UTF-8。我想当我尝试将响应数据写入响应时会发生问题。

@GET
@Path("/")
@Produces({"application/xml;charset=UTF-8","application/json;charset=UTF-8"})
public String getPersons(@Context HttpServletRequest request, @Context HttpServletResponse response) {

    List<Person> persons = personService.getPersons(testUserId, collectionOption, null);
    if (persons == null) {
        persons = new ArrayList<Person>();
    }

    String result = JsonUtil.listToJson(persons);
    //result doesnt has any encoding problem at this line

    response.setContentType("application/json");
    response.setContentLength(result.length());
    response.setCharacterEncoding("utf-8");

    //i guess problem happen after this line
    return result;
}

有没有jetty配置或者resteasy配置?或者有什么办法可以解决这个问题吗?感谢您的帮助。

I am trying to implement restful protocol on jetty server. I have runnable server and i can access it from my rest client. My server side project is a maven project. I have a problem about the character encoding.When i check response, before send it from controller, there is no encoding problem. But after i return response to client, i see broken data. Response header is UTF-8. Also i have a listener for this problem and i am setting to request and response to UTF-8. I guess problem happens when i try to write my response data to response.

@GET
@Path("/")
@Produces({"application/xml;charset=UTF-8","application/json;charset=UTF-8"})
public String getPersons(@Context HttpServletRequest request, @Context HttpServletResponse response) {

    List<Person> persons = personService.getPersons(testUserId, collectionOption, null);
    if (persons == null) {
        persons = new ArrayList<Person>();
    }

    String result = JsonUtil.listToJson(persons);
    //result doesnt has any encoding problem at this line

    response.setContentType("application/json");
    response.setContentLength(result.length());
    response.setCharacterEncoding("utf-8");

    //i guess problem happen after this line
    return result;
}

Is there any jetty configuration or resteasy configuration for it? Or is there any way to solve this problem? Thanks for your helps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慢慢从新开始 2024-11-10 05:34:39

您使用的是哪个 Resteasy 版本? 2.0.1 及之前版本中的字符串存在一个已知问题 (RESTEASY-467)。

这些是你的选择:

1)强制编码返回 byte[]

public byte[] getPersons

,然后

return result.getBytes("UTF8");

2)返回 List(或者如果需要的话创建一​​个 PersonListing)

public List<Person> getPersons

并让 Resteasy 处理 json 转换。

3) 返回 StreamingOutput

注意:使用此选项,“Content-Length”标头将是未知的。

return new StreamingOutput()
{

    public void write(OutputStream outputStream) throws IOException, WebApplicationException
    {
        PrintStream writer = new PrintStream(outputStream, true, "UTF-8");
        writer.println(result);
    }
};

4) 升级到2.2-beta-1或更高版本。

Which resteasy version are you using? There is a known issue (RESTEASY-467) with Strings in 2.0.1 an prior.

These are your options:

1) force the encoding returning byte[]

public byte[] getPersons

and then

return result.getBytes("UTF8");

2) return List (or create a PersonListing if you need it)

public List<Person> getPersons

and let resteasy handle the json transformation.

3) return a StreamingOutput

NOTE: with this option the "Content-Length" header will be unknown.

return new StreamingOutput()
{

    public void write(OutputStream outputStream) throws IOException, WebApplicationException
    {
        PrintStream writer = new PrintStream(outputStream, true, "UTF-8");
        writer.println(result);
    }
};

4) upgrade to 2.2-beta-1 or newer version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文