使用 RESTEasy / jax-rs 进行字符编码响应
我在我的服务器上使用 RESTeasy for jax-rs 进行设置。我的客户端发送一个包含字符“✓”的字符串,服务器可以存储该字符(我可以确认它是否正确存储在服务器上)。但是,服务器似乎无法在响应中返回“✓” - 而是返回“?”被发送。
我假设我需要指定返回编码或其他内容,但我不知道在哪里执行此操作,也不知道如何检查当前编码是什么!
如何指定服务器上的编码以便我可以在响应中返回“✓”?
编辑以添加代码
我的服务器代码:
@Path("compiled/{rootReportGroupId}")
@GET
@Produces("text/html; charset=UTF-8")
@NoCache
public String getCompiledReports(@PathParam("rootReportGroupId") Long rootReportGroupId){
return "✓";
}
示例请求:
GET http://192.168.0.12:8888/rest/reports/compiled/190
Host 192.168.0.12:8888
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection keep-alive
Content-Type application/json
响应标头:
Cache-Control public, no-transform, no-cache
Content-Type text/html;charset="UTF-8"
Content-Length 1
Server Jetty(6.1.x)
响应正文:
?
I'm set up using RESTeasy for jax-rs on my server. My client sends a string containing the character '✓', and the server can store that character (I can confirm that it is being stored correctly on the server). However, the server can't seem to return the '✓' in a response - instead, a '?' gets sent.
I'm assuming I need to specify a return encoding or something, but I don't know where to do this, or how to check to see what the current encoding is!
How do I specify the encoding on my server so that I can return a '✓' in a response?
edit to add code
My server code:
@Path("compiled/{rootReportGroupId}")
@GET
@Produces("text/html; charset=UTF-8")
@NoCache
public String getCompiledReports(@PathParam("rootReportGroupId") Long rootReportGroupId){
return "✓";
}
A sample request:
GET http://192.168.0.12:8888/rest/reports/compiled/190
Host 192.168.0.12:8888
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection keep-alive
Content-Type application/json
The response headers:
Cache-Control public, no-transform, no-cache
Content-Type text/html;charset="UTF-8"
Content-Length 1
Server Jetty(6.1.x)
The response body:
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有点漫无目的而且很长,所以我把它放入答案中,但这主要是一条评论。
出于好奇,您使用什么版本的 Java、Rest Easy、编译器设置?
我使用了您在 MacOS 10.6、RestEasy 2.2.3.GA、Java 1.6.0_29、Tomcat 7.0.22 上发布的代码,并且它工作正常(我删除了参数部分,但它似乎不相关)。
服务器端读写的代码是什么?读取时是否存在编码问题?
我也对您的响应标头表示怀疑,特别是:
我认为应该是:
A bit rambling and long so I put it into an answer, but it is mostly a comment.
Out of curiosity, what versions of Java, Rest Easy, compiler settings are you using?
I used your code you posted here on MacOS 10.6, RestEasy 2.2.3.GA, Java 1.6.0_29, Tomcat 7.0.22, and it worked correctly (I removed the param piece, but it doesn't seem relevant).
What is the code used to read and write on the server side? Are there encoding issues reading?
I'm also suspicious of your response headers, particularly:
I think should be:
需要配置三个层:
JSP
HTML
JSP
Servlet 中的相同类型的事物。请参阅此答案中的 JBoss 特定解决方案以及完整的服务器独立解决方案。
您可能会丢失数据库级别的角色信息。检查并确保您的数据库编码也是 UTF-8,而不是 ASCII。
有关此主题的完整讨论,请参阅 Java 文章 来自浏览器的字符转换到数据库。
There are three layers to configure:
JSP
HTML
JSP
Same type of thing in a Servlet. See JBoss specific solution as well as complete server independent solution in this answer.
You may be losing the character information at the database level. Check to make sure your database encoding is UTF-8 also, and not ASCII.
For a complete discussion of this topic, refer to Java article Character Conversions from Browser to Database.
我认为问题在于您的 IDE/文本编辑器以另一种编码保存文件,因此您使容器返回 UTF-8 编码,但文本不是这样的编码,这导致问题发生。
问候
六安
I think the problem is that your IDE/Text Editor is saving the file in another encoding, so you making the container return the UTF-8 encoding but the text isn't it that enconding, that makes the problem happens.
Regards
Luan