
Jersey, framework para construção de webservices RESTful baseado na definição JSR 311 & JSR 339, faz uso do framework Jackson para que os recursos retornem dados no formato Json. Porém, eu tive a necessidade de usar o framework Gson para converter os objetos em Json. Mas como fazer isso se o Jersey usa o Jackson implicitamente? Encontrei a resposta nesse site:
http://eclipsesource.com/blogs/2012/11/02/integrating-gson-into-a-jax-rs-based-application/
Basta criar uma classe provedora que implemente as interfaces javax.ws.rs.ext.MessageBodyWriter e javax.ws.rs.ext.MessageBodyReader e possua a annotation @Provider. Nessa nossa classe realizaremos a conversão dos de formato objeto para formato json e vice-versa, e para isso usaremos o framework Gson, substituindo assim o Jackson.
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStream; | |
import java.io.OutputStreamWriter; | |
import java.lang.annotation.Annotation; | |
import java.lang.reflect.Type; | |
import javax.ws.rs.Consumes; | |
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.MessageBodyReader; | |
import javax.ws.rs.ext.MessageBodyWriter; | |
import javax.ws.rs.ext.Provider; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
@Provider | |
@Produces(MediaType.APPLICATION_JSON) | |
@Consumes(MediaType.APPLICATION_JSON) | |
public final class GsonMessageBodyHandler implements MessageBodyWriter, MessageBodyReader { | |
private static final String UTF_8 = "UTF-8"; | |
private Gson gson; | |
private Gson getGson() { | |
if (gson == null) { | |
final GsonBuilder gsonBuilder = new GsonBuilder(); | |
gson = gsonBuilder.create(); | |
} | |
return gson; | |
} | |
@Override | |
public boolean isReadable(Class type, Type genericType, java.lang.annotation.Annotation[] annotations, MediaType mediaType) { | |
return true; | |
} | |
@Override | |
public Object readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, InputStream entityStream) throws Exception, ApplicationException { | |
InputStreamReader streamReader = new InputStreamReader(entityStream, UTF_8); | |
try { | |
Type jsonType; | |
if (type.equals(genericType)) { | |
jsonType = type; | |
} else { | |
jsonType = genericType; | |
} | |
return getGson().fromJson(streamReader, jsonType); | |
} finally { | |
streamReader.close(); | |
} | |
} | |
@Override | |
public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { | |
return true; | |
} | |
@Override | |
public long getSize(Object object, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { | |
return –1; | |
} | |
@Override | |
public void writeTo(Object object, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { | |
OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8); | |
try { | |
Type jsonType; | |
if (type.equals(genericType)) { | |
jsonType = type; | |
} else { | |
jsonType = genericType; | |
} | |
getGson().toJson(object, jsonType, writer); | |
} finally { | |
writer.close(); | |
} | |
} | |
} |