我使用Springfox从Spring Boot REST控制器生成了Swagger API规范。
我注意到一个问题,示例值/模型无法显示响应。
作为调查,我在http://localhost:8080/v2/api-文档,并在https://editor.swagger.io/,它也无法显示示例值/模型。这似乎是由于模式没有正确引用模型对象(“Car”)造成的。
但是从Swagger的API文档中(https://docs.swagger.io/swagger-core/v1.5.0/apidocs/io/swagger/annotations/ApiResponse.html#response()),它表示注释@ApiResponse的“response”属性应该对应于规范的“schema”字段。
通过指定response=“Object.class”,Swagger UI不应该相应地填充示例值/模型吗?
欢迎任何建议,如果我有任何错误配置/误解,请纠正,非常感谢。
@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));
}
型号:
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;
}
}
Swagger UI:
Springfox依赖pom.xml:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
为使用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));
}
型号:
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;
}
}
Swagger UI:
在使用springdoc openapi ui的情况下
(
@Operation(summary = "Send some JSON")
@ApiResponses(value = {
@ApiResponse(
responseCode = "200",
description = "Success: our action has been completed",
content = @Content(mediaType = "application/json",
schema = @Schema(
type = "SampleHttpResponseDto",
example = "{\"status\":\"OK\",\"message\":\"sample OK answer\"}")))})
@PostMapping(value = "/resource", consumes = MediaType.APPLICATION_JSON_VALUE)
public SampleHttpResponseDto postRequest(
@Parameter(
name ="json",
schema = @Schema(
description = "additional description of the model",
type = "string",
example = "{\"status\":\"OK\",\"message\":\"message body\"}"))
@RequestBody Map<String, Object> request
) {
return new SampleHttpResponseDto(request.propert1, request.propert2);
}
主旨:https://gist.github.com/antukhov/7dece86c6d16cc81bb6f83f47ffc0c8d
大摇大摆的样子会像这样
您可以使用V2
模型覆盖V3
模型。只需在应用程序中添加一个属性。属性
和您的@ApiResponse
注释应该可以正常工作。
springfox.documentation.swagger.use-model-v3=false
确保使用较旧的@ApiResponse
和@ApiResponse
注释。这一问题已记录在案https://github.com/springfox/springfox/issues/3503
如何仅显示类型为GET in Swagger page的API并隐藏其他API?我发现属性ApiExplorerSettings(IgnoreApi=true)可以从Swagger页面隐藏API,但是我有很多API要隐藏,我需要一种根据其HTTP类型隐藏API的方法。我尝试过这种方法: 但没用
我试图在我的微服务项目中生成一个单独的招摇过市,在Api网关中将所有服务招摇过市聚合成一个单独的招摇过市。为了实现这一点,我将遵循下一个教程https://objectpartners.com/2017/09/28/aggregate-services-into-a-single-swagger 这里的问题是,当我尝试设置绝对URL时,我收到的输出是未能加载API定义。未定义的http://loc
目前我正在编写我的API文档,我决定用ReDoc来实现它。我的API使用Spring Boot构建..对于文档,我使用SWAGGER。一切都很好,但是我无法注释我的控制器来显示ReDoc文档右边的“响应样本”部分。我尝试在dto中添加示例,如: 这是我的控制器的外观:
我的pom.xml 招摇过市配置 服务器日志 它说映射: 但这些都不起作用(404): 如果我使用sping-fox较低版本,那么我将在我的日志中得到它已映射{[/v2/api-docs}],方法=[GET]。但是,我看不到在那里生成的任何json。
我正在使用swagger doc为我的应用程序生成API。doc是用yml编写的,在某些地方它将定义一些枚举道具(我们使用mysql)。这看起来像: 我希望得到这样的东西: 正如您所看到的,类型字段将是一个在配置文件中定义的字符串,但我收到的是这个字符串: Swagger将第一个值设置为数据类型,这是绝对错误的。所以问题是如何为“type”字段获取值“string”。
我试图通过Swagger UI记录在供应商产品(WSO2 ESB)中开发的现有API服务,以供公司内部使用。供应商产品不支持招摇过市。我计划以编程方式检查/处理API服务的源代码(编写在供应商产品中),并在中生成一个包含swagger定义文件的目录/文件夹/库。json或。yml格式。那很好,我能做到。 这些api定义文件中的每一个都将在swagger UI中很好地呈现,我正在考虑使用https: