20

Jersey seems to fail when returning JSON...
This:

@GET
@Produces( MediaType.APPLICATION_JSON + ";charset=UTF-8")
public List<MyObject> getMyObjects() {
    return ....;
}

is needed to return JSON utf-8 encoded. If I use only

@Produces( MediaType.APPLICATION_JSON)

fails and for example German umlaute (üöä), will be returned in a wrong way.

Two questions:
1 - For JSON utf-8 ist standard - why not with Jersey?
2 - Can I set utf-8 for the whole REST-Servlet if a JSON Request comes in?

I am using Jersey 1.5 and CRest 1.0.1 on Android...

Mike Mitterer
  • 6,810
  • 4
  • 41
  • 62
  • How are they returned? Can you show some sample output? – Chris Salij Apr 03 '11 at 19:17
  • It returns Löwe instead of Löwe (Lion in english) but as said if I use @Produces( MediaType.APPLICATION_JSON + ";charset=UTF-8") it works... – Mike Mitterer Apr 05 '11 at 06:52
  • I am really the only one who has this problem? – Mike Mitterer Apr 12 '11 at 05:44
  • Well you've solved your own issue then? Just add the charset to the end. It seems to be the client thats displaying the data rather than Jersey. – Chris Salij Apr 12 '11 at 18:34
  • Yes, this was the solution for me but it looks a bit odd to me. Is this really the only way to handle this problem? – Mike Mitterer Apr 13 '11 at 06:27
  • I think that it's the client not the server thats having the problem. The client probably doesn't not default to utf-8, and it's only when jersey explicitly states that it's giving back utf-8 content that the client changes it's behaviour. I could be wrong, but I think that's what is going wrong. – Chris Salij Apr 13 '11 at 10:44
  • @WhiteMike: I am facing the same issue while receiving the input. I tried `@Consumes( "*/* ; charset=UTF-8")` but still getting incorrect result. When I pass **Herman Höhn** from cREST, I am getting it as **Herman H÷hn**. Please help receiving the issue. – Vijay Krish Nov 27 '12 at 17:32

3 Answers3

20

SRGs suggestion works like a charm. However, since Jersey 2.0 the interfaces are slightly different, so we had to adapt the filter a little bit:

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;

import javax.ws.rs.core.MediaType;

public class CharsetResponseFilter implements ContainerResponseFilter {
    @Override
    public void filter(ContainerRequestContext request, ContainerResponseContext response) {
        MediaType type = response.getMediaType();
        if (type != null) {
            String contentType = type.toString();
            if (!contentType.contains("charset")) {
                contentType = contentType + ";charset=utf-8";
                response.getHeaders().putSingle("Content-Type", contentType);
            }
        }
    }
}
Community
  • 1
  • 1
martin
  • 1,185
  • 17
  • 22
  • 2
    Thanks for the update! I just started out with using Jersey and stumbled upon this problem, your solution worked flawlessly! Note to Grizzly users: To register this filter, you simply need to call `rc.register(com.example.CharsetResponseFilter.class);` in your startup method (I'm using the generated boilerplate code from the tutorial). – DHainzl Oct 02 '15 at 11:54
9

I had the same problem : i don't like adding the charset in the "@Produces" tag everywhere.

I found the solution right here : http://stephen.genoprime.com/2011/05/29/jersey-charset-in-content-type.html

Basically, you just have to add a response filter that will add the charset (for example if the content type currently returned is either text, xml or json)

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;

import javax.ws.rs.core.MediaType;

public class CharsetResponseFilter implements ContainerResponseFilter {

    public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {

        MediaType contentType = response.getMediaType();
        response.getHttpHeaders().putSingle("Content-Type", contentType.toString() + ";charset=UTF-8");

        return response;
    }
}

And to register the filter :

ServletAdapter jerseyAdapter = new ServletAdapter();
jerseyAdapter.addInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters", "com.my.package.MyResponseFilter"); 

Works too with Guice, of course, for example in your class extending ServletModule :

final Map<String, String> parameters = new HashMap<String, String>();
parameters.put("com.sun.jersey.spi.container.ContainerResponseFilters", com.package.JerseyCharsetResponseFilter.class.getName());
serve("/*").with(GuiceContainer.class, parameters);
SRG
  • 1,569
  • 1
  • 17
  • 19
  • The full, more detailed source of how they implement this [can be found here](https://github.com/tinkerpop/rexster/blob/master/rexster-server/src/main/java/com/tinkerpop/rexster/filter/HeaderResponseFilter.java), and allows for the case where a method has already set the "Content-Type", thus allowing for overrides where needed. – Brad Parks Apr 11 '14 at 12:57
3

SRGs and martins solution worked well for me.

However, I had to apply the following changes to the filter:

If the client does a request with an Accept header, Jersey adds a quality factor to the content type. This looks as follows:

No problem: request without Accept header:

curl -i http://www.example.com/my-rest-endpoint

response.getMediaType().toString() is application/json. We can simply append ;charset=utf-8.

Problem: request with Accept header:

curl -i -H "Accept: application/json" http://www.example.com/my-rest-endpoint

response.getMediaType().toString() is {application/json, q=1000}. We cannot simply append ;charset=utf-8, since this would lead to the following exception:

java.lang.IllegalArgumentException: Error parsing media type '{application/json, q=1000};charset=utf-8'
    at org.glassfish.jersey.message.internal.MediaTypeProvider.fromString(MediaTypeProvider.java:92) ~[na:na]
    at org.glassfish.jersey.message.internal.MediaTypeProvider.fromString(MediaTypeProvider.java:60) ~[na:na]
    at javax.ws.rs.core.MediaType.valueOf(MediaType.java:179) ~[na:na]
    ...
Caused by: java.text.ParseException: Next event is not a Token
    at org.glassfish.jersey.message.internal.HttpHeaderReader.nextToken(HttpHeaderReader.java:129) ~[na:na]
    at org.glassfish.jersey.message.internal.MediaTypeProvider.valueOf(MediaTypeProvider.java:110) ~[na:na]
    at org.glassfish.jersey.message.internal.MediaTypeProvider.fromString(MediaTypeProvider.java:90) ~[na:na]
    ... 193 common frames omitted

I'd suggest the following code to solve this problem:

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MediaType;

public class CharsetResponseFilter implements ContainerResponseFilter {

    @Override
    public void filter(ContainerRequestContext request, ContainerResponseContext response) {
        MediaType type = response.getMediaType();
        if (type != null) {
            if (!type.getParameters().containsKey(MediaType.CHARSET_PARAMETER)) {
                MediaType typeWithCharset = type.withCharset("utf-8");
                response.getHeaders().putSingle("Content-Type", typeWithCharset);
            }
        }
    }
}
rzueger
  • 1,707
  • 1
  • 13
  • 13