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

发布于 2024-12-17 12:48:41 字数 1157 浏览 1 评论 0原文

我在我的服务器上使用 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 技术交流群。

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

发布评论

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

评论(3

野味少女 2024-12-24 12:48:41

有点漫无目的而且很长,所以我把它放入答案中,但这主要是一条评论。

出于好奇,您使用什么版本的 Java、Rest Easy、编译器设置?

我使用了您在 MacOS 10.6、RestEasy 2.2.3.GA、Java 1.6.0_29、Tomcat 7.0.22 上发布的代码,并且它工作正常(我删除了参数部分,但它似乎不相关)。

服务器端读写的代码是什么?读取时是否存在编码问题?

我也对您的响应标头表示怀疑,特别是:

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

我认为应该是:

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

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:

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

I think should be:

Content-Type    text/html;charset=UTF-8
情痴 2024-12-24 12:48:41

如何指定服务器上的编码以便我可以返回“✓”
在回应中?

需要配置三个层:

  1. 浏览器显示和表单提交

JSP

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

HTML

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

JSP

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

Servlet 中的相同类型的事物。请参阅此答案中的 JBoss 特定解决方案以及完整的服务器独立解决方案。

  1. 数据库设置

您可能会丢失数据库级别的角色信息。检查并确保您的数据库编码也是 UTF-8,而不是 ASCII。

有关此主题的完整讨论,请参阅 Java 文章 来自浏览器的字符转换到数据库

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

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

HTML

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

JSP

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

Same type of thing in a Servlet. See JBoss specific solution as well as complete server independent solution in this answer.

  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.

For a complete discussion of this topic, refer to Java article Character Conversions from Browser to Database.

为你鎻心 2024-12-24 12:48:41

我认为问题在于您的 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

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