[jax-rs-spec users] [jsr339-experts] Example 5: Why WriteInterceptors are cool

From: Bill Burke <>
Date: Fri, 27 Apr 2012 13:45:40 -0400

Example 5 revolves around enveloping an entity. For example, SMIME is
an envelope format. With SMIME you are still marshalling the entity,
but the entity is part of a multipart/signed document, or is encrypted
(or both).

public Customer getCustomer(...) {

return customer;


Here we're saying that we're returning a JAXB object that we want
enveloped within SMIME encryption. Now, you *could* implement this as a
filter by doin ghits:

Name-Bound Filter
    @Context Producers producer;

    filter() {
       MessageBodyWriter entityWriter = ...;

       SMIME smime = ...;

       OutpuStream smimeStream = smime.getOutputStream();

       entityWriter.writeTo(smimeStream, ...);


BUT...what if you had annotations on getCustomer() that triggered other
Filters? What if those filters decorated a header, post-processed the
entity, especially encoded the entity, etc.? encodings or header
decorations that you want applied to the BodyPart, but not the entire
SMIME entity. How could you handle this? I don't think you could. A
WriterInterceptor approach is much cleaner in this case.

Bill Burke
JBoss, a division of Red Hat