简体   繁体   English

Jboss RestEasy不使用UTF-8编码

[英]Jboss RestEasy doesn't use the UTF-8 encoding

I'm working with Jboss EAP 6.2 with RestEasy library ( resteasy-jaxrs 2.2.1.GA ) in Linux enviroment The body of each client service's response does not use the UTF-8 encoding. 我正在Linux环境中使用带有RestEasy库( resteasy-jaxrs 2.2.1.GA )的Jboss EAP 6.2 ,每个客户端服务的响应主体均未使用UTF-8编码。 The following a client's code example: 以下是客户的代码示例:

ClientRequest request = new ClientRequest(properties.getProperty("sgprof.service.url"));
request.accept("application/json;charset=UTF-8");
request.queryParameter("key", "value");
ClientResponse<String> response = request.post(String.class);
String jsonResponse = response.getEntity();

I have setted the UTF-8 encoding in `standalone.sh' of JBoss passing them to JVM. 我已经在JBoss的“ standalone.sh”中设置了UTF-8编码,并将其传递给JVM。 Below the arguments passed to JVM, can you help me please. 在传递给JVM的参数下面,请您能帮我。 Thanks. 谢谢。

awt.toolkit = sun.awt.X11.XToolkit
    file.encoding = UTF-8
    file.encoding.pkg = sun.io
    file.io.encoding = UTF-8
    file.separator = /
    http.nonProxyHosts = 10.*|*.local|localhost|127.*|idpc*
    http.proxyPort = 8080
    java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment
    java.awt.headless = true
    java.class.path = /home/jboss/jboss-eap-6.2/jboss-modules.jar
    java.class.version = 51.0
    java.endorsed.dirs = /usr/java/jdk1.7.0_51/jre/lib/endorsed
    java.ext.dirs = /usr/java/jdk1.7.0_51/jre/lib/ext:/usr/java/packages/lib/ext
    java.home = /usr/java/jdk1.7.0_51/jre
    java.io.tmpdir = /tmp
    java.library.path = /usr/java/default:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
    java.net.preferIPv4Stack = true
    java.runtime.name = Java(TM) SE Runtime Environment
    java.runtime.version = 1.7.0_51-b13
    java.specification.name = Java Platform API Specification
    java.specification.vendor = Oracle Corporation
    java.specification.version = 1.7
    java.util.logging.manager = org.jboss.logmanager.LogManager
    java.vendor = Oracle Corporation
    java.vendor.url = http://java.oracle.com/
    java.vendor.url.bug = http://bugreport.sun.com/bugreport/
    java.version = 1.7.0_51
    java.vm.info = mixed mode
    java.vm.name = Java HotSpot(TM) 64-Bit Server VM
    java.vm.specification.name = Java Virtual Machine Specification
    java.vm.specification.vendor = Oracle Corporation
    java.vm.specification.version = 1.7
    java.vm.vendor = Oracle Corporation
    java.vm.version = 24.51-b03
    javaEncoding = UTF-8
    javax.management.builder.initial = org.jboss.as.jmx.PluggableMBeanServerBuilder
    javax.xml.datatype.DatatypeFactory = __redirected.__DatatypeFactory
    javax.xml.parsers.DocumentBuilderFactory = __redirected.__DocumentBuilderFactory
    javax.xml.parsers.SAXParserFactory = __redirected.__SAXParserFactory
    javax.xml.stream.XMLEventFactory = __redirected.__XMLEventFactory
    javax.xml.stream.XMLInputFactory = __redirected.__XMLInputFactory
    javax.xml.stream.XMLOutputFactory = __redirected.__XMLOutputFactory
    javax.xml.transform.TransformerFactory = __redirected.__TransformerFactory
    javax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema = __redirected.__SchemaFactory
    javax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom = __redirected.__XPathFactory
    jboss.home.dir = /home/jboss/jboss-eap-6.2
    jboss.modules.dir = /home/jboss/jboss-eap-6.2/modules
    jboss.modules.system.pkgs = org.jboss.byteman
    jboss.server.base.dir = /home/jboss/jboss-eap-6.2/standalone
    jboss.server.config.dir = /home/jboss/jboss-eap-6.2/standalone/configuration
    jboss.server.data.dir = /home/jboss/jboss-eap-6.2/standalone/data
    jboss.server.deploy.dir = /home/jboss/jboss-eap-6.2/standalone/data/content
    jboss.server.log.dir = /home/jboss/jboss-eap-6.2/standalone/log
    jboss.server.persist.config = true
    jboss.server.temp.dir = /home/jboss/jboss-eap-6.2/standalone/tmp
    logging.configuration = file:/home/jboss/jboss-eap-6.2/standalone/configuration/logging.properties
    module.path = /home/jboss/jboss-eap-6.2/modules
    org.jboss.boot.log.file = /home/jboss/jboss-eap-6.2/standalone/log/server.log
    org.jboss.resolver.warning = true
    org.xml.sax.driver = __redirected.__XMLReaderFactory
    os.arch = amd64
    os.name = Linux
    os.version = 2.6.32-431.el6.x86_64
    path.separator = :
    sun.arch.data.model = 64
    sun.boot.class.path = /usr/java/jdk1.7.0_51/jre/lib/resources.jar:/usr/java/jdk1.7.0_51/jre/lib/rt.jar:/usr/java/jdk1.7.0_51/jre/lib/sunrsasign.jar:/usr/java/jdk1.7.0_51/jre/lib/jsse.jar:/usr/java/jdk1.7.0_51/jre/lib/jce.jar:/usr/java/jdk1.7.0_51/jre/lib/charsets.jar:/usr/java/jdk1.7.0_51/jre/lib/jfr.jar:/usr/java/jdk1.7.0_51/jre/classes
    sun.boot.library.path = /usr/java/jdk1.7.0_51/jre/lib/amd64
    sun.cpu.endian = little
    sun.cpu.isalist = 
    sun.io.unicode.encoding = UnicodeLittle
    sun.java.command = /home/jboss/jboss-eap-6.2/jboss-modules.jar -mp /home/jboss/jboss-eap-6.2/modules -jaxpmodule javax.xml.jaxp-provider org.jboss.as.standalone -Djboss.home.dir=/home/jboss/jboss-eap-6.2 -Djboss.server.base.dir=/home/jboss/jboss-eap-6.2/standalone -Dfile.encoding=UTF-8 -Dfile.io.encoding=UTF-8 -DjavaEncoding=UTF-8 -Dsun.io.unicode.encoding=UnicodeLittle -Dsun.jnu.encoding=UTF-8 -c standalone.xml
    sun.java.launcher = SUN_STANDARD
    sun.jnu.encoding = UTF-8
    sun.management.compiler = HotSpot 64-Bit Tiered Compilers
    sun.os.patch.level = unknown
    user.dir = /home/jboss/jboss-eap-6.2/bin
    user.home = /home/jboss
    user.language = en

I had a similar problem reading (HTTP-GET) a resource with RestEasy using the JaxRS client API. 我使用JaxRS客户端API使用RestEasy读取(HTTP-GET)资源时遇到类似的问题。

The method getCharset of org.jboss.resteasy.plugins.providers.jsonp.AbstractJsonpProvider is used at the client to determine the charset. 客户端使用org.jboss.resteasy.plugins.providers.jsonp.AbstractJsonpProvider的getCharset方法确定字符集。 As parameter mediaType the "Content-Type" of the Response-Header is used: 作为参数mediaType,使用了响应头的“内容类型”:

   public static Charset getCharset(final MediaType mediaType)
   {
      if (mediaType != null)
      {
         String charset = mediaType.getParameters().get("charset");
         if (charset != null) return Charset.forName(charset);
      }
      return Charset.defaultCharset();
   }

The Charset.defaultCharset() returns windows-1252 on my machine. Charset.defaultCharset()返回我的计算机上的Windows-1252。 In comparison, the org.jboss.resteasy.plugins.providers.ProviderHelper uses allways UTF-8 as default: 相比之下,org.jboss.resteasy.plugins.providers.ProviderHelper使用allways UTF-8作为默认值:

String charset = mediaType.getParameters().get("charset");
if (charset != null) return new String(bytes, charset);
else return new String(bytes, "UTF-8");

I found the following solutions: 我发现以下解决方案:

  • Set the system property file.encoding="UTF-8" 设置系统属性file.encoding =“ UTF-8”

  • Add @Produces(MediaType.APPLICATION_XML + ";charset=utf-8") to the jax-rs resource (if you are the owner) so that the character-set declared at with Content-Type at the response-header. 将@Produces(MediaType.APPLICATION_XML +“; charset = utf-8”)添加到jax-rs资源(如果您是所有者),以便在响应标题处使用Content-Type声明的字符集。

  • Read the response as String and parse it manually (Json.createReader(new StringReader(jsonString)).readObject()) 以String形式读取响应并手动解析(Json.createReader(new StringReader(jsonString))。readObject())

You can specify the encoding in your service declaration.. Check the @Produces annotation in the following example: 您可以在服务声明中指定编码。.在以下示例中检查@Produces批注:

@GET
@Path("/customer/{customerUrn}")
@Produces(MediaType.APPLICATION_XML + ";charset=utf-8")
public Response getCustomerProducts(@PathParam("customerUrn")String customer);

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM