tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From iluvtr <ilu...@gmail.com>
Subject Re: TomEE JAX-RS MessageBodyWriter / Reader not working like the standard
Date Mon, 20 Oct 2014 01:20:57 GMT
I solved guys!, it just was a matter of adding  writer.flush(); at the end of
the method 'writeTo'

But I don't remember I added that line to plain Tomcat, well, it's solved
anyway, 

The code is here maybe some of you find it useful


@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class GsonJsonProvider<T> implements MessageBodyReader<T>,
MessageBodyWriter<T> {

    private Gson gson;

    public GsonJsonProvider() {
        gson = new Gson();
    }

    @Override
    public boolean isReadable(Class<?> type, Type genericType, Annotation[]
annotations, MediaType mediaType) {
        return true;
    }

    @Override
    public T readFrom(Class<T> type, Type genericType, Annotation[]
annotations, MediaType mediaType, MultivaluedMap<String, String>
httpHeaders,
            InputStream entityStream) throws IOException,
WebApplicationException {
        InputStreamReader streamReader = new InputStreamReader(entityStream,
StandardCharsets.UTF_8);
        return gson.fromJson(streamReader, type);
    }

    @Override
    public boolean isWriteable(Class<?> type, Type genericType, Annotation[]
annotations, MediaType mediaType) {
        return true;
    }

    @Override
    public long getSize(T t, Class<?> type, Type genericType, Annotation[]
annotations, MediaType mediaType) {
        return -1;
    }

    @Override
    public void writeTo(T t, Class<?> type, Type genericType, Annotation[]
annotations,
            MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException, WebApplicationException {
        OutputStreamWriter writer = new OutputStreamWriter(entityStream,
StandardCharsets.UTF_8);
        gson.toJson(t, writer);
        writer.flush();  //This line is  the path to success!
    }

}




--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/TomEE-JAX-RS-MessageBodyWriter-Reader-not-working-like-the-standard-tp4672454p4672455.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Mime
View raw message