1

A strange thing happens with us, when using Jersey 2.19 on GlassFish 4.1.

We are currently migrating old servlet based json services to JAX-RS. With the old servlets, we didn't have any problem with encoding (used JAXB/MOXy for marshalling / unmarshalling).

Now, given a specific service:

@GET
@Produces(MediaType.APPLICATION_JSON + "; charset=UTF-8")
public Help getHelp( @PathParam("pageId") String pageId, @PathParam("language") String language, 
        @Context HttpServletRequest request, 
        @HeaderParam("accept-language") String userLanguage ) {

The client receives a HTTP response, but with some missing headers:

Response:
HTTP/1.1 200 OK
Server: GlassFish Server Open Source Edition  4.1
x-powered-by: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition  4.1  Java/Oracle Corporation/1.7)
Content-Type: application/json
Date: Tue, 13 Oct 2015 11:28:06 GMT
Content-Length: 3453

For the following request:

Request headers:
GET ... HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: hu-HU,en-US;q=0.8,hu;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/json; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: ...
Cookie: JSESSIONID=0ef43870235def33391f93b81818; __utma=111872281.648188082.1392392895.1392392895.1392392895.1; ice.push.browser=1ifp52zip; ice.connection.lease=1444735687588; ice.connection.contextpath=.; ice.connection.running=9621a:acquired
Connection: keep-alive

So despite the fact, that UTF-8 was requested, the response we get is interpreted by mozilla as not UTF-8, so we don't see the special language characters correctly (like 'á', 'é', 'ő', etc.).

Strange is when we directly build the response, with responsebuilder:

final Response myResponse = Response.status(Response.Status.OK)
            .entity(entity)
            .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_TYPE.withCharset("utf-8"))
            .build();

We get the following response header:

HTTP/1.1 200 OK
Server: GlassFish Server Open Source Edition  4.1
x-powered-by: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition  4.1  Java/Oracle Corporation/1.7)
Content-Type: application/json; charset=utf-8
Date: Tue, 13 Oct 2015 11:33:57 GMT
Content-Length: 103

So it seems like that the Jersey only puts UTF-8 charset in the response header, when directly instructed.

Do you know any other option, how to tell directly in the Jersey response, to send data with charset=utf-8? And without using the responsebuilder?

Thanks!

csabee
  • 217
  • 2
  • 12
  • 1
    _"So despite the fact, that UTF-8 was requested"_ - Um no. Only the `Content-Type` is specifying the charset. This is the type of data being sent by the client. The `Accept` header is what tells the server what type you want back. So set the charset in the `Accept`. From what I tested, it should work. – Paul Samsotha Oct 13 '15 at 11:52
  • Nice catch, I will test it. – csabee Oct 13 '15 at 13:27

0 Answers0