Jersey character / default character encoding

Jersey seems to fail on returning JSON ...
It:

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

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

 @Produces( MediaType.APPLICATION_JSON) 

will fail, and, for example, the German umlaut (รผรถรค), will be returned incorrectly.

Two questions:
1 - Standard for JSON utf-8 standard - why not with Jersey?
2 - Can I install utf-8 for the entire REST servlet if a JSON request arrives?

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

+16
android rest jersey servlets
Apr 01 2018-11-11T00:
source share
3 answers

The SRG offer works like a charm. However, the interfaces are slightly different with Jersey 2.0, so we had to adapt the filter a 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); } } } } 
+15
Dec 13 '13 at 15:03
source share

I had the same problem: I donโ€™t like adding the encoding to the @Produces tag everywhere.

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

Basically, you just need to add a response filter that adds an encoding (for example, if the returned content type is 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 register the filter:

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

Also works 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); 
+6
Jan 29 '13 at 14:30
source share

SRG and martins solution worked well for me.

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

If a customer executes an Accept header request, Jersey adds a quality factor to the content type. It looks like this:

No problem: request without Accept header:

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

response.getMediaType().toString() application/json . We can just add ;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 just add ;charset=utf-8 , as this will result in 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 would 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); } } } } 
+3
Mar 14 '15 at 18:26
source share



All Articles