Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey / Rest default character encoding

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...

like image 867
Mike Mitterer Avatar asked Apr 01 '11 13:04

Mike Mitterer


2 Answers

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);
            }
        }
    }
}
like image 192
martin Avatar answered Oct 07 '22 13:10

martin


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);
like image 27
SRG Avatar answered Oct 07 '22 13:10

SRG