简体   繁体   English

使用RESTEasy / jax-rs编码响应的字符

[英]character encoding responses with RESTEasy / jax-rs

I'm set up using RESTeasy for jax-rs on my server. 我在我的服务器上使用RESTeasy设置jax-rs。 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: 响应机构:

?

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? 出于好奇,您使用的是什么版本的Java,Rest Easy,编译器设置?

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). 我使用了你在MacOS 10.6,RestEasy 2.2.3.GA,Java 1.6.0_29,Tomcat 7.0.22上发布的代码,它运行正常(我删除了参数,但似乎没有相关性)。

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: 我也怀疑你的回复标题,特别是:

Content-Type    text/html;charset="UTF-8"

I think should be: 我认为应该是:

Content-Type    text/html;charset=UTF-8

How do I specify the encoding on my server so that I can return a '✓' in a response? 如何在服务器上指定编码,以便在响应中返回“✓”?

There are three layers to configure: 要配置三个层:

  1. Browser Display and Form Submission 浏览器显示和表单提交

JSP JSP

<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>

HTML HTML

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  1. Web Server Processing Web服务器处理

JSP JSP

<%
  request.setCharacterEncoding("UTF-8");
  String name = request.getParameter("NAME");
%>

Same type of thing in a Servlet. Servlet中的相同类型的东西。 See JBoss specific solution as well as complete server independent solution in this answer . 此答案中,请参阅JBoss特定解决方案以及完整的服务器独立解决方案。

  1. Database Settings 数据库设置

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. 检查以确保您的数据库编码也是UTF-8,而不是ASCII。

For a complete discussion of this topic, refer to Java article Character Conversions from Browser to Database . 有关此主题的完整讨论,请参阅Java文章从浏览器到数据库的字符转换

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. 我认为问题在于您的IDE /文本编辑器正在以另一种编码保存文件,因此您使容器返回UTF-8编码,但文本不是那个包含,这会导致问题发生。

Regards Luan 关心栾

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

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