Java <;Spring Boot/Springfox>;虚张声势用户界面未显示示例值和模型
我使用Springfox从Spring Boot REST控制器生成了Swagger API规范 我注意到一个无法显示示例值/模型以供响应的问题 作为调查,我在http://localhost:8080/v2/api-博士, 并将其转换为YMAL at,它无法同时显示示例值/模型。 这似乎是由于模式没有正确引用模型对象(“Car”)造成的 但是从Swagger()的API文档中可以看出,注释@ApiResponse的“response”属性应该对应于规范的“schema”字段 通过指定response=“Object.class”,Swagger UI不应该相应地填充示例值/模型吗 欢迎提供任何建议,如果我有任何错误配置/误解,请予以纠正,非常感谢Java <;Spring Boot/Springfox>;虚张声势用户界面未显示示例值和模型,java,spring-boot,swagger,openapi,springfox,Java,Spring Boot,Swagger,Openapi,Springfox,我使用Springfox从Spring Boot REST控制器生成了Swagger API规范 我注意到一个无法显示示例值/模型以供响应的问题 作为调查,我在http://localhost:8080/v2/api-博士, 并将其转换为YMAL at,它无法同时显示示例值/模型。 这似乎是由于模式没有正确引用模型对象(“Car”)造成的 但是从Swagger()的API文档中可以看出,注释@ApiResponse的“response”属性应该对应于规范的“schema”字段 通过指定re
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));
}
大摇大摆的用户界面:
pom.xml中的Springfox依赖项:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
大摇大摆的用户界面:
您可以使用
V2
模型覆盖V3
模型。只需在应用程序中添加一个属性。属性
和@ApiResponse
注释应该可以正常工作
springfox.documentation.swagger.use-model-v3=false
确保使用较旧的@ApiResponse
和@ApiResponse
注释。
在使用
springdoc openapi ui
(>=1.5.0)的情况下,这个问题已被记录在案,这是我的工作示例,用于显示SwaggerUI的请求和响应部分中的示例数据(如果是JSON对象)。
希望它也能对你的情况做一些小改动
@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);
}
@操作(summary=“发送一些JSON”)
@ApiResponses(值={
@蜂群反应(
responseCode=“200”,
description=“成功:我们的行动已经完成”,
content=@content(mediaType=“application/json”,
schema=@schema(
type=“SampleHttpResponseDto”,
示例=“{\'status\':\'OK\',\'message\':\'sample OK answer\'”)})
@PostMapping(value=“/resource”,consumes=MediaType.APPLICATION\u JSON\u value)
公共样本HttpResponsedToPostRequest(
@参数(
name=“json”,
schema=@schema(
description=“模型的附加说明”,
type=“string”,
示例=“{\”状态\“:\”确定\“,\”消息\“:\”消息正文\“}”))
@请求体映射请求
) {
返回新的SampleHttpResponseDto(request.propert1,request.propert2);
}
要点:
SwaggerUI与Springfox 3.x中的类似,注释应该是
@ApiResponse(…,content={@content(mediaType=“application/json”,schema=@schema(implementation=Car.class))}
。看见Javadoc就在这里:Hi@Helen,我已经尝试过使用OAS3.0规范和注释(参考上面的更新),但它似乎仍然不起作用。
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;
}
}
springfox.documentation.swagger.use-model-v3=false
@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);
}