【问题标题】:Swagger defination, how to specify that a file is returned?Swagger定义,如何指定返回一个文件?
【发布时间】:2017-03-06 04:43:27
【问题描述】:

我想从服务器下载一个文件,我定义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
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);
    }

}

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

【问题讨论】:

    标签: file-io swagger restful-architecture swagger-ui


    【解决方案1】:

    使用InputStreamResource返回文件内容:

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

    【讨论】:

    • 是的,我知道使用 inputstreamresource 会返回文件内容。但是我应该如何在第一个文件中定义 restful api?现在根据我在第一个文件中的定义,它会生成一个返回 ResponseEntity 而不是 ResponseEntity 的函数,就像在第二个文件中一样...
    • 直接返回一个文件作为响应没有多大意义。正如您所发现的,File 对象实际上只是文件系统上实际文件的路径和元数据的容器,它不包含文件内容。因此,如果您向用户发送一个File("/root/dev/file.txt") 对象,他们将获得一个File 对象,但它将指向他们的 文件系统上的路径/root/dev/file.txt。文件对象对传输文件没有用,只有关于文件的信息(比如它们在哪里)。
    • 呃,也许你还没有理解我的意思:) 我已经知道 ResponseEntity 不符合我们的意图。但是,根据 Swagger api 定义,它生成的 api 代码只返回 ResponseEntity 而不是 ResponseEntity。其实第二个代码文件不是我写的。它是由 Swagger 代码生成器生成的。:D 那么你知道如何在第一个文件中编写正确的 api 定义吗?
    • 您找到解决方案了吗?我现在也有类似的问题。
    • 有什么替代解决方案?
    猜你喜欢
    • 1970-01-01
    • 2022-09-29
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 2021-10-22
    相关资源
    最近更新 更多