How to set encoding using JAX-RS?

How to set encoding using JAX-RS? I tried @Produces("text/html; charset=UTF-8") , but this was ignored and only the text/html message was sent with an HTTP header. I want to set the encoding in MessageBodyWriter, but I do not want to extract the media type by analyzing the @Produces annotation through reflection from myself.

+46
java java-ee character-encoding jax-rs
Aug 7 '10 at 20:52
source share
7 answers

As Demon noted in a comment, recent versions of JAX-RS (including the stable version as of September 2012) now do support @Produces syntax. So you can just use:

 @Produces("text/html; charset=UTF-8") 
+59
Sep 26
source share
— -

You can also use the ResponseBuilder.header (...) method to set the content type using encoding. The following is an example code (using JAX-RS 1.1.1, CXF 2.3.1).

 final Response myResponse = Response.status(Response.Status.BAD_REQUEST) .entity("La requête n'est pas correcte.\n ...") .header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN+"; charset=ISO-8859-15" ) .build(); 
+15
Aug 30 2018-11-21T00:
source share

If you want to do this in a neutral implementation mode of JAX-RS, you can reset the Content-Type in MessageBodyWriter. Something like:

 public void writeTo(Object obj, Class<?> cls, Type type, Annotation[] annotations, MediaType mt, MultivaluedMap<String, Object> responseHttpHeaders, OutputStream stream) throws IOException { responseHttpHeaders.putSingle(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, mt.toString() + ";charset=UTF-8"); } 

If you have different character sets besides UTF-8 for each resource method, you can create a custom annotation and add it to each resource method. Then try using the annotation parameter in the writeTo () method.

Just FYI, Apache Wink supports the use of encoding and other attributes for media types. Hopefully future JAX-RS spec specifications will facilitate this.

+9
Aug 09 '10 at 10:01
source share

Just to keep abreast of the latest developments. Not sure if this was supported in older versions of Jersey, but definitely if you decide to use the ResponseBuilder.header (...) method, you can use the MediaType withCharset () method. Like this:

 return Response.status(Status.OK) .entity(result) .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_TYPE.withCharset("utf-8")) .build()); 
+9
Jul 31 '15 at 8:39
source share

What I am doing is getting an instance of the servlet response object:

 protected @Context HttpServletResponse response; 

And then set the character encoding to utf-8:

 response.setCharacterEncoding("utf-8"); 

This works for me.

+1
Aug 24 '16 at 13:01
source share

First setting up @Produces annotations for your resource class methods.

Then in the MessageBodyWriter your return type, you can do this in the writeTo() method:

 response.setContentType(mediaType.toString); 

Note. You can enter response in your writer with:

 @Context protected HttpServletResponse response; 
0
Aug 23 '16 at 9:40
source share

If you use RESTEasy, you can register an Inteceptor:

 import org.jboss.resteasy.annotations.interception.ServerInterceptor; import org.jboss.resteasy.core.ResourceMethodInvoker; import org.jboss.resteasy.core.ServerResponse; import org.jboss.resteasy.spi.Failure; import org.jboss.resteasy.spi.HttpRequest; import org.jboss.resteasy.spi.interception.PreProcessInterceptor; import org.jboss.resteasy.plugins.providers.multipart.InputPart; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.ws.rs.WebApplicationException; import javax.ws.rs.ext.Provider; @Provider @ServerInterceptor public class ContentTypeSetter implements PreProcessInterceptor { @Override public ServerResponse preProcess(HttpRequest request, ResourceMethodInvoker resourceMethodInvoker) throws Failure, WebApplicationException { request.setAttribute(InputPart.DEFAULT_CONTENT_TYPE_PROPERTY, "*/*; charset=UTF-8"); return null; } } 

Note. If you manually install @Produces, it overrides the ContentType set by this interceptor. If you do, set the encoding to @Produces

0
Dec 07 '16 at 16:41
source share



All Articles