JAXRS jersey filter that provides an unmarshalled entity before hitting a resource (web service)

Is there an elegant way (filters, interceptors) to intercept an unmarshalled object (and its annotations) in Jersey before it gets into the web service resource methods - those that are annotated with @POST, @PUT.

I really need the entity itself and any annotations that it has on it, and then test on this object using the validation annotation properties (looks like JSR 303, but we do not annotate the entire bean with metadata). From the point of view of resource implementation, my goal is to be able to simply type this in the resource method:

@Path("/people") @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) public class PeopleService { @POST public Response createPerson(@CustomValidator("personValidator") Person person) throws URISyntaxException { String uri = someService.createPerson(person); return Response.created(new URI(uri)).entity(uri).build(); } } 

I started the path of writing a custom MessageBodyReader, and I was able to fully run JSON because I was simply delegating ObjectMapper to deserialize JSON. Other than that, I passed parameter annotations and everything works fine. However, when I needed to deserialize XML, everything fell apart. I have no idea how to get the original deserializer that Jersey would use and delegate to him. I also feel that I overwrite the pieces of Jersey unnecessarily and would prefer to stop using MessageBodyReader as a hacking integration point if the jersey has a layer after MessageBodyReader and before the implementation calls the web resource service, which allows it to be extended so that I can add a custom one flow behavior.

Ultimately, does Jersey have any additional points where it can pass me the unmarshalled entity and any of its parameter annotations and allow me to do custom processing? Or do I need to figure out how to create entities myself (using JAXBContext and what not)? Perhaps resorting to AOP? Embed validator in resource class and abandon this annotation idea?

Jersey Version: 1.5

+7
source share
2 answers

Ultimately, Jersey has some extra points where it can pass me an unmarshalled entity and any of them are parameter annotations and let me do custom processing?

Yes, there is SPI in Jersey 1.x where you can provide your own invoker method. I have never implemented SPI, but I understand that it was called after any MessageBodyReaders or filters. Take a look at ResourceMethodCustomInvokerDispatchProvider . From Javadocs:

The implementation (service provider) identifies itself by placing the provider configuration file (if it is not already present), "Com.sun.research.ws.rest.spi.invoker.ResourceMethodCustomInvokerDispatchProvider" in the META-INF / services resource directory, including completely qualified implementation provider provider class in a file.

This interface is similar to ResourceMethodDispatchProvider, but allows you to use a custom instance of JavaMethodInvoker, which will be used to make the final call to the Java method.

If you implement ResourceMethodCustomInvokerDispatchProvider, you can provide your own JavaMethodInvoker , which will have access to the called method of the resource, including all its parameters and annotations.

Please note that all of the above applies to Jersey 1.x. In Jersey 2, ResourceMethodInvocationHandlerProvider provides similar functionality.

+2
source
0
source

All Articles