【问题标题】:How is data from HTTP request body passed into a servlet [duplicate]HTTP请求正文中的数据如何传递到servlet [重复]
【发布时间】:2020-04-22 13:41:52
【问题描述】:

我是 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) {
    //.... 
  }

【问题讨论】:

    标签: java spring spring-boot jersey jax-rs


    【解决方案1】:

    我在这里找到了答案:https://stackoverflow.com/a/1773308/9867856。事实证明,只要方法用@POST 标记,方法的String 参数就会自动填充来自请求正文的数据。

    【讨论】:

      猜你喜欢
      • 2012-10-05
      • 1970-01-01
      • 2016-10-18
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 2018-01-04
      • 1970-01-01
      • 2019-08-25
      相关资源
      最近更新 更多