gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:20:12 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com