Riley Lark
Riley Lark

Reputation: 20890

character encoding responses with RESTEasy / jax-rs

I'm set up using RESTeasy for jax-rs on my server. My client sends a string containing the character '✓', and the server can store that character (I can confirm that it is being stored correctly on the server). However, the server can't seem to return the '✓' in a response - instead, a '?' gets sent.

I'm assuming I need to specify a return encoding or something, but I don't know where to do this, or how to check to see what the current encoding is!

How do I specify the encoding on my server so that I can return a '✓' in a 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 "✓";
}

A sample request:

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

The response headers:

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

The response body:

?

Upvotes: 5

Views: 10319

Answers (3)

gknicker
gknicker

Reputation: 5569

How do I specify the encoding on my server so that I can return a '✓' in a response?

There are three layers to configure:

  1. 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");
%>

Same type of thing in a Servlet. See JBoss specific solution as well as complete server independent solution in this answer.

  1. Database Settings

You may be losing the character information at the database level. Check to make sure your database encoding is UTF-8 also, and not ASCII.

For a complete discussion of this topic, refer to Java article Character Conversions from Browser to Database.

Upvotes: 2

lcestari
lcestari

Reputation: 178

I think the problem is that your IDE/Text Editor is saving the file in another encoding, so you making the container return the UTF-8 encoding but the text isn't it that enconding, that makes the problem happens.

Regards Luan

Upvotes: 0

Charlie
Charlie

Reputation: 7349

A bit rambling and long so I put it into an answer, but it is mostly a comment.

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

I used your code 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 removed the param piece, but it doesn't seem relevant).

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

I'm also suspicious of your response headers, particularly:

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

I think should be:

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

Upvotes: 3

Related Questions