Swagger定义,如何指定返回文件?

时间:2016-10-24 11:59:31

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

我想从服务器下载文件,并按如下方式定义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);
    }

}

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

1 个答案:

答案 0 :(得分:0)

使用InputStreamResource返回文件内容:

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