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...
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...
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