gpt4 book ai didi

file-io - Swagger定义,如何指定返回一个文件?

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

我想从服务器下载一个文件,我定义swagger文件如下:

swagger: '2.0'

################################################################################
# API Information
################################################################################
info:
version: v0
title: XXX REST API

host: api.xxx.io
basePath: /v0

schemes:
- http
- https
produces:
- application/json

################################################################################
# Security
################################################################################


################################################################################
# Parameters
################################################################################
parameters:
productId:
name: productId
in: path
description: The product identifier
type: string
required: true

################################################################################
# Paths
################################################################################
paths:
/products:
get:
description: Get the list of products
operationId: getProducts
responses:
200:
description: OK
schema:
type: array
items:
$ref: '#/definitions/Product'

/resources/{productId}:
parameters:
- $ref: '#/parameters/productId'
get:
description: Get resources of a product
operationId: getResourcesByProductId
produces:
- application/octet-stream
responses:
200:
description: OK
schema:
type: file

################################################################################
# Definitions
################################################################################
definitions:
Product:
type: object
required:
- id
properties:
id:
type: string
name:
type: string
category:
type: array
items:
type: string
description:
type: string
price:
type: number
thumbnailUri:
type: string
previewUris:
type: array
items:
type: string
resources:
type: array
items:
$ref: '#ResourceMeta'


api如下:

@javax.annotation.Generated(value = "class io.swagger.codegen.languages.SpringCodegen", date = "2016-10-24T17:56:03.446+08:00")

@Controller
public class ResourcesApiController implements ResourcesApi {

public ResponseEntity<File> getResourcesByProductId(
@ApiParam(value = "The product identifier",required=true ) @PathVariable("productId") String productId


) {
// do some magic!
return new ResponseEntity<File>(HttpStatus.OK);
}

}


我的 Controller 如下:

@Controller
public class ResourceController implements ResourcesApi {

private final Logger logger = Logger.getLogger(ResourceController.class);

// @RequestMapping(value="/resources/{productId}", method= RequestMethod.GET)
public ResponseEntity<File> getResourcesByProductId(@ApiParam(value = "The product identifier", required = true) @PathVariable("productId") String productId) {
String path = "resources" + File.separator + productId;
File file = new File(path);
FileSystemResource fileSystemResource = new FileSystemResource(file);
InputStreamResource inputStreamResource = null;
try {
inputStreamResource = new InputStreamResource(fileSystemResource.getInputStream());
} catch (IOException e) {
logger.error(e.toString());
}

HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName()));

return ResponseEntity
.ok()
.headers(headers)
.contentLength(file.length())
.body(file);
}

}


但是,当我运行应用程序时,它返回一个文件,但只包含文件的元数据而不是其内容。我怎样才能让它返回文件内容?谢谢!

最佳答案

使用 InputStreamResource返回文件内容:

return new ResponseEntity(inputStreamResource, headers, HttpStatus.OK);

关于file-io - Swagger定义,如何指定返回一个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40217843/

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