Created
March 6, 2015 14:30
-
-
Save michalbcz/f2ff7a909035f089cdb3 to your computer and use it in GitHub Desktop.
resteasy - automatically encode output of JSON media type (application/json) as UTF-8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.ws.rs.Produces; | |
import javax.ws.rs.WebApplicationException; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.MultivaluedMap; | |
import javax.ws.rs.ext.MessageBodyWriter; | |
import javax.ws.rs.ext.Provider; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.lang.annotation.Annotation; | |
import java.lang.reflect.Type; | |
@Provider | |
@Produces(MediaType.APPLICATION_JSON) | |
public class JsonStringProducer implements MessageBodyWriter<String> { | |
@Override | |
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { | |
return String.class.equals(type); | |
} | |
@Override | |
public long getSize(String s, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { | |
return -1; | |
} | |
@Override | |
public void writeTo(String s, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { | |
entityStream.write(s.getBytes("UTF-8")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment