Character encoding with RESTEasy / jax-rs

I am configured to use RESTeasy for jax-rs on my server. My client sends a string containing the character "โœ“", and the server can save this character (I can confirm that it is stored correctly on the server). However, the server does not seem to be able to return "โœ“" in response - instead, "?" heading off.

I suppose I need to specify a reverse encoding or something like that, but I don't know where to do this, or how to check what the current encoding is!

How to specify the encoding on my server so that I can return "โœ“" in response?

edit to add code

My server code:

@Path("compiled/{rootReportGroupId}") @GET @Produces("text/html; charset=UTF-8") @NoCache public String getCompiledReports(@PathParam("rootReportGroupId") Long rootReportGroupId){ return "โœ“"; } 

Request example:

 GET http://192.168.0.12:8888/rest/reports/compiled/190 Host 192.168.0.12:8888 User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:7.0.1) Gecko/20100101 Firefox/7.0.1 Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language en-us,en;q=0.5 Accept-Encoding gzip, deflate Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7 Connection keep-alive Content-Type application/json 

Answer headers:

 Cache-Control public, no-transform, no-cache Content-Type text/html;charset="UTF-8" Content-Length 1 Server Jetty(6.1.x) 

Answer body:

 ? 
+4
source share
3 answers

A bit incoherent and long, so I put it back, but this is mostly a comment.

Out of curiosity, which versions of Java, Rest Easy, the compiler settings are you using?

I used your code that you posted here on MacOS 10.6, RestEasy 2.2.3.GA, Java 1.6.0_29, Tomcat 7.0.22, and it worked correctly (I deleted part of the parameter, but this does not seem relevant).

What is the code used to read and write on the server side? Are there any encoding issues?

I am also suspicious of your response headers, in particular:

 Content-Type text/html;charset="UTF-8" 

I think it should be:

 Content-Type text/html;charset=UTF-8 
+3
source

How to specify the encoding on my server so that I can return 'โœ“' in the response?

There are three layers to configure:

  • Browser display and form submission

Jsp

 <%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> 

HTML

 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  1. Web server processing

Jsp

 <% request.setCharacterEncoding("UTF-8"); String name = request.getParameter("NAME"); %> 

The same thing in the servlet. See the specific JBoss solution, as well as the complete server independent solution in this answer .

  1. Database settings

You may lose character information at the database level. Check that your database encoding is also UTF-8, not ASCII.

For a full discussion of this topic, see the Java article Converting Characters from Browser to Database .

+2
source

I think the problem is that your IDE / Text editor saves the file in a different encoding, so you make the container return UTF-8 encoding, but the text does not support this, which makes the problem.

Relationship luan

0
source

Source: https://habr.com/ru/post/1382590/


All Articles