We are using JAX-RS with some fairly basic POJO entities and have a number of @GET and @POST annotated methods that @Produce and @Consume MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML. Nothing spectacular.
Question I have is how best should I validate the data coming in?
We do not have an XML Schema although I could generate one. I would need to hook this in somehow which doesn’t look terribly appealing and I have not found a concise example yet.
We could use “bean validation” but I am again unsure how I might hook this up and invoke it.
Finally (I think) we could for example add some isValidForXXX() methods to the entity POJOs and call them whenever we have an instance handed in to us.
Recommendations anyone?
If you have an XML schema then you could use JAXB validation inside a
MessageBodyReader. For a concrete example see my answer to a similar question.ValidatingReader
Below is a bare bones implementation of
MessageBodyReaderthat does four things: 1)Create a JAXBContext, 2) Create an instance ofSchema, 3) Sets the schema on theUnmarshaller4) Unmarshals theInputStream.