We have built some REST (jax-rs) web services using Apache CXF. They return a JSON response.
I now need to modify some of the request parameters, and response content. (Basically we need to encode/encrypt some of the data that is returned by the service; and decode/decrypt the same data when it is used as a parameter in a subsequent service call.)
It seems I have at least 4 options here:
- Use a Servlet filter
- Use a CXF Interceptor
- Use a JAX-RS Filter
- Don’t use any particular pattern, and perform the encode/decode within the actual service logic.
I’ve used Servlet Filters before, and understand exactly how to modify request params and response body, so I’m leaning toward that. However, I’m open to using a CXF Interceptor or JAX-RS filter if that is the more ‘correct’ way to solve this when using CXF. But based on the documentation, I don’t really understand how to do this. For example, do I use the setContent method of the Message object to change the JSON response? What is the format parameter in that case, just String.class?
Answering my own question here … I ended up using a JAX-RS filter, and it worked well, once I got past the lack of documentation. I used the (rather sparse) documentation from http://cxf.apache.org/docs/jax-rs-filters.html . Note despite it’s name, a JAX-RS filter is a CXF-specific beast, not part of the JAX-RS standard (as far as I can tell).
Here is some example code:
I should note that the implementation of MyOutputStreamWrapper is the important part in modifying the response content. I couldn’t include that source here (in fact my implementation has a different name) due to security considerations.