jersey web service json utf-8 encoding(jersey web 服务 json utf-8 编码)
问题描述
我使用 Jersey 1.11 制作了一个小型 Rest web 服务.当我调用返回 Json 的 url 时,非英文字符的字符编码存在问题.Xml 的对应 url ("test.xml" 使其在起始 xml-tag 中为 utf-8.
I made a small Rest webservice using Jersey 1.11. When i call the url that returns Json, there are problems with the character encoding for non english characters. The corresponding url for Xml ("test.xml" makes it utf-8 in the starting xml-tag.
如何让 url "test.json" 返回 utf-8 编码的响应?
How can I make the url "test.json" return utf-8 encoded response?
这是服务的代码:
@Stateless
@Path("/")
public class RestTest {
@EJB
private MyDao myDao;
@Path("test.xml/")
@GET
@Produces(MediaType.APPLICATION_XML )
public List<Profile> getProfiles() {
return myDao.getProfilesForWeb();
}
@Path("test.json/")
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Profile> getProfilesAsJson() {
return myDao.getProfilesForWeb();
}
}
这是服务使用的 pojo:
This is the pojo that the service uses:
package se.kc.mimee.profile.model;
@XmlRootElement
public class Profile {
public int id;
public String name;
public Profile(int id, String name) {
this.id = id;
this.name = name;
}
public Profile() {}
}
推荐答案
默认情况下,Jersey 应该始终生成 utf-8,听起来问题是您的客户端没有正确解释它(xml 声明不会使" 它是 utf-8,只是告诉客户端如何解析它).
Jersey should always produce utf-8 by default, sounds like the problem is that your client isn't interpreting it correctly (the xml declaration doesn't "make" it utf-8, just tells the client how to parse it).
您发现这些问题的客户是什么?
What client are you seeing these problems with?
有效的 JSON 应该只是 Unicode (utf-8/16/32);解析器应该能够自动检测到编码(当然有些不会),所以 JSON 中没有编码声明.
Valid JSON is only supposed to be Unicode (utf-8/16/32); parsers should be able to detect the encoding automatically (of course, some don't), so there is no encoding declaration in JSON.
您可以像这样将它添加到 Content-Type 中:
You can add it to the Content-Type like so:
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
这篇关于jersey web 服务 json utf-8 编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:jersey web 服务 json utf-8 编码
基础教程推荐
- 从 python 访问 JVM 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 大摇大摆的枚举 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
