球衣网络服务 json utf-8 编码 [英] jersey web service json utf-8 encoding

查看:28
本文介绍了球衣网络服务 json utf-8 编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Jersey 1.11 制作了一个小型 Rest 网络服务.当我调用返回 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")

这篇关于球衣网络服务 json utf-8 编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆