spring-boot - 使用 Open API 进行测试时,带有 HttpEntity 的 Spring Rest Controller API 获取空主体

标签 spring-boot swagger openapi springdoc springdoc-openapi-ui

我正在使用 Spring Boot 2.6.7 和使用 Open API springdoc-openapi-ui 1.6.4。我有 2 项服务。从第一项服务开始,我使用休息模板连接到第二项服务。 在第一个服务中,在 rest controller api 中,我使用了 HttpEntity获取请求对象。同样传递给 rest 模板。原因在于 HttpEntity,我正在传递请求正文以及一些其他 header 。

我的 Controller 方法如下。

@PostMapping(value = "/submit", produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "API for submit", description = "Submit data")
@ApiResponses(value = { @ApiResponse(responseCode = "200", description = "OK"),
        @ApiResponse(responseCode = "400", description = "Bad request", content = @Content(schema = @Schema(implementation = Failure.class))),
        @ApiResponse(responseCode = "500", description = "Error", content = @Content(schema = @Schema(implementation = Failure.class))), })
public ResponseEntity<Success<SubmitOpr>> submit(HttpEntity<OperationReq> httpEntity) throws Exception {
    log.info("Request Entity is {}", httpEntity);
    log.info("Request Body is {}", httpEntity.getBody());
    SuccessResponse<SubmitOpr> response = null;
    try {
        response = oprService.submit(httpEntity);
    } catch (Exception e) {
        log.error("Failure: {}", e.getMessage());
        throw e;
    }
    return ResponseEntity.ok().body(response);
}

我的应用程序可以正常工作。 postman 客户端也可以正常工作。 但是当我使用 swagger UI 进行测试时,并没有得到预期的结果。当我调试时,httpEntity.getBody() 为 null

如果我从 HttpEntity<OperationReq> httpEntity 改变至 OperationReq httpEntity然后相应地更改后续服务层方法,该 api 可以很好地工作。 但我不想改变这一点。因为我想传递 HttpEntity,另一件事是有太多类似的 API,很难在任何地方更改。

有更好的解决方案吗?

最佳答案

我认为对此没有直接的解决方案。如果您希望获取 header 和请求正文,您可以做的是,您可以获取 RequestBody 并在 Controller 中获取 Headers 并创建一个 HttpEntity 对象。并且您可以传递给您的服务层方法。 仅在 Controller 端进行更改,服务层不需要更改。

@PostMapping(value = "/submit", produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "API for submit", description = "Submit data")
@ApiResponses(value = { @ApiResponse(responseCode = "200", description = "OK"),
        @ApiResponse(responseCode = "400", description = "Bad request", content = @Content(schema = @Schema(implementation = Failure.class))),
        @ApiResponse(responseCode = "500", description = "Error", content = @Content(schema = @Schema(implementation = Failure.class))), })
public ResponseEntity<Success<SubmitOpr>> submit(@RequestBody OperationReq operationReq, @RequestHeader MultiValueMap<String, String> headers) throws Exception {
    HttpEntity<OperationReq> httpEntity = new HttpEntity<>(operationReq, headers);
    log.info("Request Entity is {}", httpEntity);
    log.info("Request Body is {}", httpEntity.getBody());
    SuccessResponse<SubmitOpr> response = null;
    try {
        response = oprService.submit(httpEntity);
    } catch (Exception e) {
        log.error("Failure: {}", e.getMessage());
        throw e;
    }
    return ResponseEntity.ok().body(response);
}

关于spring-boot - 使用 Open API 进行测试时,带有 HttpEntity 的 Spring Rest Controller API 获取空主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73248408/

相关文章:

java - 找不到依赖项 : expected at least 1 bean 的 [HrEmployeesReportOutput] 类型的合格 bean

spring-boot - 如何使用 Netty 扩展 Spring react 堆

python - 将 API 路径与 Swagger API 路径相匹配

spring-boot - 如何使 Swagger 3 UI 以 DDMMYYYY 格式显示示例日期?

c# - 将 ISchemaFilter 示例转换为 Swashbuckle 5.0

java - 如何使自定义异常处理程序正常工作

java - 尝试上传文件,数组有问题

node.js - 如何为托管的 Swagger API 生成实时 Swagger API 页面?

c# - 如何为类生成 JSON 示例值,就像 Swagger 所做的示例响应一样?

spring-boot - OpenAPI 与 swagger