I'm new to Java web programming, and I was reading the following piece of code which uses Spring and swagger. I could not understand how the jsonRequestBody
parameter was populated with data from the HTTP POST request body. I read from somewhere that the @RequestBody
annotation should be used to read the request body, much like how you use @PathParam
and @QueryParam
, but it is not used in this case. How did Spring figure out how to parse the HTTP request body and put it in jsonRequestBody
?
@POST
@Path("{transactionId}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@ApiOperation(value = "Update Running balances for Journal Entries", notes = "This API calculates the running balances for office. If office ID not provided this API calculates running balances for all offices. \n" + "Mandatory Fields\n" + "officeId")
@ApiImplicitParams({@ApiImplicitParam(paramType = "body", value = "body", dataType = "body", dataTypeClass = JournalEntriesApiResourceSwagger.PostJournalEntriesTransactionIdRequest.class)})
@ApiResponses({@ApiResponse(code = 200, message = "", response = JournalEntriesApiResourceSwagger.PostJournalEntriesTransactionIdResponse.class)})
public String createReversalJournalEntry(@ApiParam(hidden = true) final String jsonRequestBody, @PathParam("transactionId") @ApiParam(value = "transactionId") final String transactionId,
@QueryParam("command") @ApiParam(value = "command") final String commandParam) {
//....
}
I found out the answer here at: https://stackoverflow.com/a/1773308/9867856. It turns out that as long as the method is marked with @POST, the method's String parameter will be automatically populated with data from the request body.