奇怪的字符编码

时间:2017-07-24 20:48:15

标签: java character-encoding websphere-liberty open-liberty

我有一个在Linux上的WebSphere Liberty上运行的项目。 我的语言是巴西葡萄牙语,我们有一些重音词。 我的java代码设置了一些用户消息,如下所示:

...
ErroResponse erroResponse = new ErroResponse();
erroResponse.setMensagem("Esse grupo não pode ser criado. Já existe um grupo criado com esse nome.");
response = Response.status(Status.BAD_REQUEST).entity(erroResponse).build();
...

当向用户显示相同的消息时,它看起来像这样: enter image description here

我认为这不是浏览器编码问题,因为我的服务器日志中的消息看起来是一样的。 enter image description here

我尝试使用-Dclient.encoding.override设置JVM编码 -Dfile.encoding到ISO-8859-1和UTF-8,没有成功。

在Windows服务器上运行的同一个项目运行没有问题,显示带有正确重音的消息。

只有直接写在源代码中的消息才会出现此问题。正确显示来自数据库查询结果的重音词。

我正在使用Suse 11.4。

我真的很感激任何帮助。

由于

4 个答案:

答案 0 :(得分:0)

尝试检查您的Suse编码和语言:

class Array
  def my_uniq
    hash = {}
    each do |num|
      hash[num] = 0;
    end
    hash.keys
  end
end

class Array
  def median
    arr = map {|n| n}
    puts arr.to_s
  end
end

获取所有语言:

$ echo $LC_CTYPE
ISO-8859-1

$ echo $LANG
pt_BR

更改为pt_PT.utf8:

$ locale -a

答案 1 :(得分:0)

在添加这些JVM编码属性之前,您可能会在响应中获得该字符的UTF-8编码,但您的浏览器不愿意将它们解释为UTF-8。我建议删除它们并将其保留在JAX-RS资源中。

一个有用的调试步骤是使用像客户端和十六进制编辑器(如var test = [{id: 1, name: 'foo', sport: 'soccer'}, {id: 2, name: 'bar', sport: 'basketball'}, {id: 3, name: 'acme', sport: 'basketball'}, {id: 4, name: 'xyz', sport: 'baseball'}]; test.reduce(function(aggregator, item) { if(!aggregator[item.sport]) { aggregator[item.sport] = []; } aggregator[item.sport].push({id: item.id, name: item.name}) return aggregator; }, {}); )这样的命令查看respone,因为有很多东西可以掩盖实际传输的数据(浏览器,终端仿真器等) )。

如果您明确告诉服务器使用字符集(UTF-8或ISO8859-1等本地代码页),您的字符串将转换为指定的代码页。浏览器还会在Content-Type标题中看到charset。

最简单的方法是在JAX-RS中使用@Produces,如下所示:

od -t x1

@Produces("text/html; charset=UTF-8")

答案 2 :(得分:0)

解决了改变项目文本编码的问题。默认情况下,eclipse将源代码保存为UTF-8,javac将其编译为CP1252(Windows默认值)。

要进行此更改,我必须右键单击Project - >属性 - >然后资源将文本文件编码更改为其他(ISO-8859-1)。

答案 3 :(得分:0)

我在使用maven和jax-ws服务时遇到了类似的问题,该服务返回错误字符(源代码中包含文本)。

通过将其添加到父Maven项目中来解决此问题:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
相关问题