我是 Java Web 编程新手,我正在阅读以下使用 Spring 和 swagger 的代码。我无法理解 jsonRequestBody
参数已使用 HTTP POST 请求正文中的数据填充。我从某处读到 @RequestBody
应该使用注释来读取请求正文,就像使用 @PathParam
一样和@QueryParam
,但本例中不使用它。 Spring是如何弄清楚如何解析HTTP请求体并将其放入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) {
//....
}
最佳答案
我在这里找到了答案:https://stackoverflow.com/a/1773308/9867856 。事实证明,只要该方法被标记为@POST,该方法的String参数就会自动填充请求体中的数据。
关于java - HTTP 请求正文中的数据如何传递到 servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61366623/