Я пытаюсь обработать исключения в своем файле загрузки Api. Я использую openapi: «3.0.2» в сервисе springboot. Я попытался реализовать controlleradvice, но OPEN API не поддерживает Controlleradvice. Не могли бы вы предложить стандартный подход или метод, который помогает мне обрабатывать исключения. Пожалуйста, найдите мой код удаления файла ниже.
yml файл
/namespace/{namespace}/bucket/{bucketName}/file/{fileName}:
get:
operationId: downloadFileFromEcs
summary: Download file from ECS
description: |-
Download file from ECS
security:
- x-et-auth-details: []
tags:
- ECS
parameters:
- in: path
name: bucketName
schema:
type: string
required: true
- in: path
name: namespace
schema:
type: string
required: true
- in: path
name: fileName
schema:
type: string
required: true
responses:
200:
description: Downloaded file
content:
application/octet-stream:
schema:
$ref: "#/components/schemas/DownloadFile"
400:
content:
application/json:
schema:
$ref: "#/components/schemas/Error400BadRequest"
403:
content:
application/json:
schema:
$ref: "#/components/schemas/Error403AccessDenied"
404:
content:
application/json:
schema:
$ref: "#/components/schemas/Error404NotFound"
500:
content:
application/json:
schema:
$ref: "#/components/schemas/Error500InternalServerError"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorResponse"
Rest-api. java
@ApiOperation(value = "Download file from ECS", nickname = "downloadFileFromEcs", notes = "Download file from ECS", response = org.springframework.core.io.Resource.class, tags={ "ECS", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Downloaded file", response = org.springframework.core.io.Resource.class),
@ApiResponse(code = 400, message = "", response = Error400BadRequest.class),
@ApiResponse(code = 403, message = "", response = Error403AccessDenied.class),
@ApiResponse(code = 404, message = "", response = Error404NotFound.class),
@ApiResponse(code = 500, message = "", response = Error500InternalServerError.class),
@ApiResponse(code = 200, message = "unexpected error", response = ErrorResponse.class) })
@RequestMapping(value = "/namespace/{namespace}/bucket/{bucketName}/file/{fileName}",
produces = { "application/octet-stream", "application/json" },
method = RequestMethod.GET)
default CompletableFuture<ResponseEntity<org.springframework.core.io.Resource>> _downloadFileFromEcs(@ApiParam(value = "",required=true) @PathVariable("bucketName") String bucketName
,@ApiParam(value = "",required=true) @PathVariable("namespace") String namespace
,@ApiParam(value = "",required=true) @PathVariable("fileName") String fileName
) {
return downloadFileFromEcs(bucketName, namespace, fileName);
}
// Override this method
default CompletableFuture<ResponseEntity<org.springframework.core.io.Resource>> downloadFileFromEcs(String bucketName,String namespace,String fileName) {
if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
if (getAcceptHeader().get().contains("application/json")) {
try {
return CompletableFuture.completedFuture(new ResponseEntity<>(getObjectMapper().get().readValue("\"\"", org.springframework.core.io.Resource.class), HttpStatus.NOT_IMPLEMENTED));
} catch (IOException e) {
log.error("Couldn't serialize response for content type application/json", e);
return CompletableFuture.completedFuture(new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR));
}
}
} else {
log.warn("ObjectMapper or HttpServletRequest not configured in default EcsApi interface so no example is generated");
}
return CompletableFuture.completedFuture(new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED));
}
Остаток java
@Override
public CompletableFuture<ResponseEntity<Resource>> downloadFileFromEcs(String bucketName,String namespace, String fileName) {
// Load file as Resource
logger.info("[ECS Controller] Download: Starting input stream");
InputStream inputStream = ecsService.downloadFileFromS3Bucket(bucketName, namespace, fileName);
logger.info("[ECS Controller] Download: Starting resource ");
Resource resource = new InputStreamResource(inputStream);
logger.info("[ECS Controller] setting fileDownloadResponse");
String contentType = "application/octet-stream";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.parseMediaType(contentType));
logger.info("[ECS Controller] setting http headers");
/* return CompletableFuture.completedFuture(ResponseEntity<ECSDownloadResponse>.ok()
.contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
.body(resource));*/
return CompletableFuture.completedFuture(new ResponseEntity<Resource>(
resource,httpHeaders, HttpStatus.OK));
}
ОБСЛУЖИВАНИЕ java
public InputStream downloadFileFromS3Bucket(String bucket, String namespace, String fileName) throws AmazonS3Exception {
if (!namespace.isEmpty() && namespace.equals("ac_gemsync")) {
accessKey = accessKeyGem;
secretKey = secretKeyGem;
}
if (!bucket.isEmpty()) {
bucketName = bucket;
}
String result = "";
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
AmazonS3Client s3client = new AmazonS3Client(credentials);
s3client.setEndpoint(endpointUrl);
S3ClientOptions options = new S3ClientOptions();
options.setPathStyleAccess(true);
s3client.setS3ClientOptions(options);
logger.info("[ECS Download] Download: Starting s3 download");
long startTime = System.currentTimeMillis();
S3Object object = s3client.getObject(new GetObjectRequest(bucketName, fileName));
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
logger.info("[ECS CALL] downloadFileFromS3Bucket: :" + totalTime + " milli secs");
logger.info("[ECS Download] Download: Starting input stream");
InputStream inputStream = object.getObjectContent();
logger.info("[ECS Download] Done input stream");
return inputStream;
}