<Spring Boot / Springfox> Swagger UI 未显示示例值和模型

Pat*_* C. 5 java swagger spring-boot springfox openapi

我使用 Springfox 从 Spring Boot REST 控制器生成 Swagger API 规范。

我注意到一个问题,无法显示示例值/模型以进行响应。

作为一项调查,我检查了 http://localhost:8080/v2/api-docs 处的 JSON API 文档,并将其转换为https://editor.swagger.io/处的 YMAL ,但它无法显示示例值/模型也是如此。这似乎是由于架构未正确引用模型对象(此处为“汽车”)引起的。

Swagger_Editor_01

Swagger_Editor_02

但是从Swagger的API文档(https://docs.swagger.io/swagger-core/v1.5.0/apidocs/io/swagger/annotations/ApiResponse.html#response())来看,它说“响应”注释 @ApiResponse 的属性应对应于规范的“schema”字段。

在此输入图像描述

通过指定response =“Object.class”,Swagger UI 不应该相应地填充示例值/模型吗?

欢迎大家提出建议,如有错误配置/误解,请指正,非常感谢。


REST 控制器和注释:
@GetMapping(path = "/car")
@ApiOperation(value = "Get car by color.", response = Car.class)
@ApiParam(value = "Color of the car.", required = true)
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK.", response = Car.class),
        @ApiResponse(code = 400, message = "Invalid color provided."),
        @ApiResponse(code = 404, message = "Car not found.") })
public ResponseEntity<Object> getCarByColor(@RequestParam String color) {
    return ResponseEntity.ok(testService.getCarByColor(color));
}
Run Code Online (Sandbox Code Playgroud)

模型:

package com.example.demo.model;

import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@ApiModel(value = "Car", description = "The model for car")
@Data
public class Car {
    @ApiModelProperty(notes = "Car ID.", example = "12345", required = false, position = 0)
    private Long id;

    @ApiModelProperty(notes = "Car name.", example = "Suzuki Swift 2020", required = true, position = 1)
    @NotNull
    @Max(value = 30, message = "Name can only have a maximum length of 30")
    private String name;

    @ApiModelProperty(notes = "Car color.", example = "blue", required = true, position = 2)
    @NotNull
    @Max(value = 30, message = "Color can only have a maximum length of 30")
    @Pattern(regexp = "^(blue|yellow)$", message = "Only blue or yellow color is allowed.")
    private String color;

    public Car(Long id, String name, String color) {
        this.id = id;
        this.name = name;
        this.color = color;
    }
}
Run Code Online (Sandbox Code Playgroud)

招摇用户界面:

示例_值_和模型_未显示

pom.xml 中的 Springfox 依赖:

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)




<更新(2020 年 7 月 31 日)>

进行了以下更改以使用OAS3.0规范和注释,但仍然存在问题。它还在 Swagger UI 中给出错误。

REST 控制器和注释:

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;

...
......

@GetMapping(path = "/car", produces = "application/json")
@Operation(summary = "Get car by color.", responses = {
        @ApiResponse(responseCode = "200", description = "OK.", content = {
                @Content(mediaType = "application/json", schema = @Schema(type = "object", implementation = Car.class)) }) })
public ResponseEntity<Object> getCarByColor(@RequestParam String color) {
    return ResponseEntity.ok(testService.getCarByColor(color));
}
Run Code Online (Sandbox Code Playgroud)

模型:

package com.example.demo.model;

import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@ApiModel(value = "Car", description = "The model for car")
@Schema
@Data
public class Car {
    @ApiModelProperty(notes = "Car ID.", example = "12345", required = false, position = 0)
    private Long id;

    @ApiModelProperty(notes = "Car name.", example = "Suzuki Swift 2020", required = true, position = 1)
    @NotNull
    @Max(value = 30, message = "Name can only have a maximum length of 30")
    private String name;

    @ApiModelProperty(notes = "Car color.", example = "blue", required = true, position = 2)
    @NotNull
    @Max(value = 30, message = "Color can only have a maximum length of 30")
    @Pattern(regexp = "^(blue|yellow)$", message = "Only blue or yellow color is allowed.")
    private String color;

    public Car(Long id, String name, String color) {
        this.id = id;
        this.name = name;
        this.color = color;
    }
}
Run Code Online (Sandbox Code Playgroud)

招摇用户界面:

Swagger_UI_Error_01

Swagger_UI_Error_02

Sha*_*rup 3

您可以使用V3模型覆盖V2模型。只需在您的注释中添加一个属性application.properties,您的@ApiResponse注释就应该正常工作。

springfox.documentation.swagger.use-model-v3=false
Run Code Online (Sandbox Code Playgroud)

确保使用较旧的@ApiResponses@ApiResponse注释。此问题已记录在https://github.com/springfox/springfox/issues/3503