com.buabook.http.common.jersey.JsonMessageBodyWriter Maven / Gradle / Ivy
package com.buabook.http.common.jersey;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
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 org.json.JSONObject;
/**
* Jersey Message Body Writer for {@link JSONObject}
* (c) 2016 Sport Trades Ltd
*
* @author Jas Rajasansir
* @version 1.0.0
* @since 14 Jul 2016
*/
@Produces(MediaType.APPLICATION_JSON)
public class JsonMessageBodyWriter implements MessageBodyWriter {
@Override
public boolean isWriteable(Class> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return JSONObject.class.equals(type);
}
/** As per the documentation, this function always returns -1. */
@Override
public long getSize(JSONObject t, Class> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return -1;
}
@Override
public void writeTo(JSONObject t, Class> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap httpHeaders, OutputStream entityStream)
throws IOException, WebApplicationException {
String output = "{}";
if(t != null)
output = t.toString();
entityStream.write(output.getBytes());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy