gpt4 book ai didi

java - 使用 RestTemplate 作为 Rest API 时出现 400 错误代码

转载 作者:行者123 更新时间:2023-12-01 16:15:33 25 4
gpt4 key购买 nike

我有一个 REST API 可以调用,并且我已经使用 REST 模板编写了客户端。执行时,我收到 400 状态代码。使用 POSTMAN 时,相同的 REST API 工作正常。以下是 API 和调用者的代码片段。如果有人发现任何东西,请告诉我。

用于 POST 方法的 REST API-

@ApiOperation(value = "Download repository as zip")
@ApiResponses({@ApiResponse(code = 200, message = ""), @ApiResponse(code = 400, message = "")})
@PostMapping(value = "/download", produces = {MediaType.APPLICATION_OCTET_STREAM_VALUE})
public ResponseEntity<StreamingResponseBody> downloadRepository(
@RequestBody @Validated final RepositoriesRequest repositoriesRequest) {

final Situation situation = this.situationsService.getSituationId(repositoriesRequest);
if (isNull(situation)) {
return ResponseEntity.notFound().build();
} else {
final ExtractionRequest extractionRequest = new ExtractionRequest(repositoriesRequest.getType(), situation,
repositoriesRequest.getDatabase());

if (!this.validateRequest(extractionRequest)) {
return ResponseEntity.badRequest().build();
}
final ExtractionResponse response = this.extractService.extractRepository(extractionRequest);

if (null == response) {
return ResponseEntity.notFound().build();
}
final InputStream inputStream = this.extractService.getFileFromS3(response.getRepositoryPath());

if (null == inputStream) {
return ResponseEntity.noContent().build();
}

final StreamingResponseBody bodyWriter = this.bodyWriter(inputStream);

return ResponseEntity.ok()
.header("Content-Type", "application/zip")
.header(CONTENT_DISPOSITION, "attachment; filename=\"repository-" + situation.getId() + ".zip\"")
.body(bodyWriter);
}
}

使用带有身份验证 token 和请求正文作为输入的 Rest 模板的 REST 客户端 -

HttpEntity<MultiValueMap<String, Object>> buildLoadRepoRequest(
final SimulationContext context,
final List<String> tablesName,
final String simulationId,
final Integer offset) {
final Token token = this.authenticateOkoye(simulationId, offset);
LOGGER.info("Token Run: {}", token.getAccessToken());
final String database = this.getDatabaseForEnvironment();

final HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(APPLICATION_JSON_UTF8);
httpHeaders.set(AUTHORIZATION, "Bearer " + token.getAccessToken());

final MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("database", database);
body.add("monthlyClosingMonth", context.getMonthlyClosingDate());
body.add("repositorySnapshot", context.getRepository());
body.add("situationId", context.getSituationId());
body.add("tableNames", tablesName);
body.add("type", context.getRunType());

return new HttpEntity<>(body, httpHeaders);
}

异常处理程序 -

    @Override
@ExceptionHandler(HttpClientErrorException.class)
public void loadRepository(
final SimulationContext context,
final List<String> tablesName,
final String simulationId,
final Integer offset,
final Path repositoryPath) throws IOException {
LOGGER.info("[{}] [{}] repository tablesName: {}", simulationId, offset, tablesName);
this.restTemplate.setRequestFactory(this.getClientHttpRequestFactory());
final ClientHttpResponse response = this.restTemplate.postForObject(
this.repositoriesUrl,
this.buildLoadRepoRequest(context, tablesName, simulationId, offset),
ClientHttpResponse.class);

if (response != null && HttpStatus.OK == response.getStatusCode()) {
LOGGER.info(
"response status on simulation : {} - Context: {} - status: {}",
simulationId,
offset,
response.getStatusCode());
//this.helper.copy(response.getBody(), repositoryPath);
} else if (response != null && HttpStatus.NO_CONTENT != response.getStatusCode()) {
throw new JarvisException(
"Can't retrieve RWA repository on simulation " + simulationId + " Context:" + offset);
}
}

我们从昨天起就一直在调查这个问题,但仍然没有任何线索。到目前为止,我们已经尝试过 postForEntity、exchange、将 header 更改为正确的 setter 方法,并尝试将参数作为对象传递。它们都不起作用。

在调用 API 时,我强烈感觉到 header 级别出现了问题。

最佳答案

您是否尝试使用httpHeaders.setContentType(MediaType.APPLICATION_JSON)

或者使用 APPLICATION_JSON_UTF8 值将消耗添加到 @PostMapping 注释

关于java - 使用 RestTemplate 作为 Rest API 时出现 400 错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62400255/

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