Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

This is a follow on article from Jersey JAX-RS filters and interceptors execution order for a simple GET request. This time instead of a GET request I'll be looking at a POST request.

For the sake of brevity this article builds on top of the classes already created in the earlier article and also makes use of the classes from this article - Writing a custom MessageBodyReader to process POST body data with Jersey. Please review both of these articles first if you haven't already done so.

Making a POST request to the /hello URI ends up executing the following filters and interceptors...
jerseyflow__post1.png


It's mostly the same as the execution flow for a GET request, except prior to the resource class being called there are calls to the reader interceptor and then the message body reader classes. After the resource class is run, the execution order remains the same as for a GET request.

Now what if instead of using a custom message body reader we used a standard @FormParam annotation to pass a value to the resource? Here's a new resource class that implements just that...
 Java
@Path("/hello2")
@Produces({ "text/plain" })
public class HelloPostResource2 {
@POST
@Consumes("application/x-www-form-urlencoded")
public String sayHello(@FormParam("name") String name)
{
Logger.getLogger("net.igorkromin.Logger")
.debug(">>> MyResource 2");
return "Hello " + name;
}
}




This class has a @Consumes("application/x-www-form-urlencoded") annotation on the @POST method to specify that form data is being expected. The parameter to the sayHello() method is now annotated with @FormParam and is changed to a simple String data type. The path is changed to /hello2.

Sending a POST request to the /path2 URI produces the following execution order...
jerseyflow__post2.png


As expected the message body reader is not called at all, but the reader interceptor is. This is because the message body reader that was created for the first POST resource doesn't match for the new resource and hence never gets executed.

That sums up how a POST request is processed in a standard use case. I'll be following up soon with an article on what happens during cases where an exception is thrown. Keep an eye out for future posts, I'll link them here as they are available.

-i

Skip down to comments...
Hope you found this post useful...

...so please read on! I love writing articles that provide beneficial information, tips and examples to my readers. All information on my blog is provided free of charge and I encourage you to share it as you wish. There is a small favour I ask in return however - engage in comments below, provide feedback, and if you see mistakes let me know.

If you want to show additional support and help me pay for web hosting and domain name registration, donations, no matter how small, are always welcome!

Use of any information contained in this blog post/article is subject to this disclaimer.
 
comments powered by Disqus
Other posts you may like...