but I need to know how to create (decorate) an existing MessageBodyWriters new MessageBodyWriter for my Stream
You can simply enter Providers and use getMessagBodyWriter(...) , passing the necessary data to search for a specific author for this type. for instance
@Provider public class StreamBodyWriter implements MessageBodyWriter<Stream> { @Context private Providers providers; @Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return Stream.class.isAssignableFrom(type); } @Override public long getSize(Stream stream, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return -1; } @Override public void writeTo(Stream stream, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { Object obj = stream.collect(Collectors.toList()); Class<?> objType = obj.getClass(); MessageBodyWriter writer = providers.getMessageBodyWriter(objType, null, annotations, mediaType); writer.writeTo(obj, objType, null, annotations, mediaType, httpHeaders, entityStream); } }
If you look at writeTo , I first call collect , then I get the return type. Then find the author for this type, and then just pass it on to the author.
Here is a test
@Path("stream") public class StreamResource { @GET @Produces(MediaType.APPLICATION_JSON) public Response getStream() { List<Person> myList = Arrays.asList( new Person("Stack"), new Person("Overflow"), new Person("Sam")); Stream<Person> stream = myList.stream() .filter(p -> p.name.startsWith("S")); return Response.ok(stream).build(); } public static class Person { public String name; public Person(String name) { this.name = name; } public Person() {} } }
C:\>curl -v http://localhost:8080/api/stream
Result:
[{"name":"Stack"},{"name":"Sam"}]
As an aside, if you plan to manipulate Stream in a writer, perhaps look at Interceptor . In fact, this will not affect, but if you want to adhere to the principle of single responsibility, Interceptor is used for this, which processes the request body.
Note: above standard JAX-RS
As an alternative...
In particular, with Jersey, you can also enter MessageBodyWorkers , for a more specific search, and even call it writeTo , which will delegate to the desired writer, if one of the exsists.