Spring is a great addition to your Java stack. It helps you access the request for inside your Spring beans easily. Maybe I’m doing it wrong, but reading the request body seems blasted verbose after that. It takes a surprising amount of code.
Access the Request
Spring’s bread and butter, of course, is inversion of control. So, to get to the request object itself is no big deal. Just inject it (canonical packages shown for… fun, mostly):
And now for the pile of non-abstracted code.
Parse the Body
Number one, the body is accessed from the
HttpServletRequest object it a place that wasn’t obvious to me:
request.getReader(). Heads up, we’ve got a BufferedReader here. Hey, at least we don’t get a stream that we have to wrap in more than one reader!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
The code. There is to much. Let me sum up. There’s only going to be a body if you are doing a post. Otherwise, don’t worry about it. If there is data, pull it into a string. Everything else is for (not) handling errors.
There’s a bit there. It’s not the worst. But after getting used to Express’ bodyParser, it was hard to see this.