java JAX-RS 编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5170566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:48:17  来源:igfitidea点击:

JAX-RS Encoding

javajsonrestencodingjax-rs

提问by RedEagle

I'm using JAX-RS to create a web (rest) service that returns results in JSON format.

我正在使用 JAX-RS 创建一个以 JSON 格式返回结果的 Web(休息)服务。

Everything is OK, except the encoding.

一切正常,除了编码。

For example, I get:

例如,我得到:

 ..., parameter:"Dep\u00f3sitos" ,...

Instead of:

代替:

 ..., parameter:"Depósitos" ,...

I've tried using:

我试过使用:

@Produces("application/json; charset=UTF-8")

but the problem remains. If I return it as XML using just:

但问题仍然存在。如果我仅使用以下内容将其作为 XML 返回:

@Produces("application/xml")

Everything is ok.

一切都好。

What do I need to set to produce the right type?

我需要设置什么才能生成正确的类型?

采纳答案by RedEagle

I ended up using GSON instead of IBM's JSON4J which proved to be much better at handling custom Java class serialization.

我最终使用 GSON 而不是 IBM 的 JSON4J,后者被证明在处理自定义 Java 类序列化方面要好得多。

回答by Frank

All you need is this:

你只需要这个:

String back = "Depósitos";
return new String(back.getBytes(), "UTF8");

回答by bamana

Take a look at Bryant Luk's answer to the question "How to set the charset with JAX-RS?" and see if it does the trick.

看看Bryant Luk“如何使用 JAX-RS 设置字符集?”这个问题的回答,看看它是否有用。