I am looking for a way to wrap any response of my service.
I have a simple service with many different methods like
@Path(value = "/listSomething/{apiKey:.+}") public List<ObjectA> listSomething(@PathParam(value = "apiKey") String apiKey)
Some return lists, some just some simple objects. What I want to achieve now is to wrap any of these answers surrounding them with status information (for example, below).
response { staus: "OK", data: {..the actual response..} }
I tried to achieve this with some kind of interceptor, but I was unsuccessful ( unless you add @XmlSeeAlso annotations, which I really don't want because I need a general approach ). My shell class is as follows:
@XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement //@XmlSeeAlso(ResponseA.class) public class ResponseWrapper { boolean error; String message; @XmlAnyElement(lax = true) Object object; public boolean isError() { return error; } public void setError(boolean error) { this.error = error; } public String getMessage() { return message; } }
I tried different combinations of @XmlElement, @XmlAnyElement - without any success. I tried it inside WriterInterceptor and ContainerResponseFilter.
How can I do this simple trick (it would be cool if I don't need to care if the consumer wants XML or JSON, but if I need to hack something for JSON, am I ok with that)?
I feel lost at the moment. Thanks for your help.
The error I usually get with the approaches I tried was UNKOWN_CLASS
{0} nor any of its super class is known to this context.
My interceptor does more or less this
responseContext.setGenericType(Wrapper.class); responseContext.setType(Wrapper.class); responseContext.setEntity(wrapperInstance);