Close

JAX-RS - MessageBodyReader Examples

JAX-RS JAVA EE 

Writing an Entity Provider

@Provider
@Consumes({"application/yaml", MediaType.TEXT_PLAIN})
@Produces({"application/yaml", MediaType.TEXT_PLAIN})
public class YamlEntityProvider<T> implements MessageBodyWriter<T>, MessageBodyReader<T> {

@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 {
Yaml yaml = new Yaml();
T t = yaml.loadAs(toString(entityStream), type);
return t;
}

public static String toString(InputStream inputStream) {
return new Scanner(inputStream, "UTF-8")
.useDelimiter("\\A").next();
}

@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 {
Yaml yaml = new Yaml();
OutputStreamWriter writer = new OutputStreamWriter(entityStream);
yaml.dump(t, writer);
writer.close();
}
}
Original Post




@Provider
@Consumes({"application/yaml", MediaType.TEXT_PLAIN})
@Produces({"application/yaml", MediaType.TEXT_PLAIN})
public class YamlEntityProvider<T> implements MessageBodyWriter<T>, MessageBodyReader<T> {
@Context
private Providers providers;

@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 {
ContextResolver<YamlContext> cr = providers.getContextResolver(YamlContext.class, mediaType);
YamlContext context = cr.getContext(type);
T t = context.readEntity(entityStream, type);
return t;
}

@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 {
ContextResolver<YamlContext> cr = providers.getContextResolver(YamlContext.class, mediaType);
YamlContext context = cr.getContext(type);
context.writeEntity(entityStream, t);

}
}
Original Post




See Also