spring-mvc与jetty服务器上的resteasy字符编码问题
我正在尝试在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的是哪个 Resteasy 版本? 2.0.1 及之前版本中的字符串存在一个已知问题 (RESTEASY-467)。
这些是你的选择:
1)强制编码返回 byte[]
,然后
2)返回 List(或者如果需要的话创建一个 PersonListing)
并让 Resteasy 处理 json 转换。
3) 返回 StreamingOutput
注意:使用此选项,“Content-Length”标头将是未知的。
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[]
and then
2) return List (or create a PersonListing if you need it)
and let resteasy handle the json transformation.
3) return a StreamingOutput
NOTE: with this option the "Content-Length" header will be unknown.
4) upgrade to 2.2-beta-1 or newer version.