【问题标题】:415Unsupported Media Type in endpoint generated by openapi 3.0415OpenAPI 3.0 生成的端点中不支持的媒体类型
【发布时间】:2022-12-21 21:56:32
【问题描述】:

我使用使用表单数据的 openapi 3.0 生成了端点。不知道我做错了什么,因为它全部生成并且在过去我已经能够上传这样的文件。不同的是,现在我除了文件之外还有多个字段。

paths:
  /movie:
    post:
      operationId: createMovie
      description: creates movie
      requestBody:
        content:
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/MovieRequest'
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Movie'


电影请求组件:

MovieRequest:
      type: object
      properties:
        title:
          type: string
        description:
          type: string
        director:
          type: string
        length:
          type: integer
          format: int64
        category:
          $ref: '#/components/schemas/Category'
        ageCategory:
          $ref: '#/components/schemas/AgeCategory'
        poster:
          type: string
          format: binary
        trailerLink:
          type: string
        shortDescription:
          type: string

生成的控制器:

 @ApiOperation(value = "", nickname = "createMovie", notes = "creates movie", response = MovieModelApi.class, tags={  })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "", response = MovieModelApi.class) })
    @RequestMapping(
        method = RequestMethod.POST,
        value = "/movie",
        produces = { "application/json" },
        consumes = { "multipart/form-data" }
    )
    default ResponseEntity<MovieModelApi> createMovie(@ApiParam(value = "") @Valid @RequestPart(value = "title", required = false) String title,@ApiParam(value = "") @Valid @RequestPart(value = "description", required = false) String description,@ApiParam(value = "") @Valid @RequestPart(value = "director", required = false) String director,@ApiParam(value = "", allowableValues = "HORROR") @Valid @RequestPart(value = "category", required = false) CategoryModelApi category,@ApiParam(value = "", allowableValues = "PG13") @Valid @RequestPart(value = "ageCategory", required = false) AgeCategoryModelApi ageCategory,@ApiParam(value = "") @Valid @RequestPart(value = "poster", required = false) MultipartFile poster) {
        getRequest().ifPresent(request -> {
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    String exampleString = "{ \"director\" : \"director\", \"isEnabled\" : true, \"description\" : \"description\", \"id\" : 5, \"title\" : \"title\", \"poster\" : \"poster\" }";
                    ApiUtil.setExampleResponse(request, "application/json", exampleString);
                    break;
                }
            }
        });
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }

将 yaml 导入邮递员后,我发送请求:

包含标题:

但是我收到 415 unsupported media 错误

【问题讨论】:

    标签: spring spring-boot rest postman openapi


    【解决方案1】:

    HTTP 415 Unsupported Media Type 表示服务器拒绝接受请求,因为不支持负载格式。格式问题可能与请求中指定的 Content-Type 或 Content-Encoding 有关,或者是直接验证数据的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-10
      • 2019-08-02
      • 1970-01-01
      • 2019-05-01
      相关资源
      最近更新 更多