Spring boot Swagger声明schema=@schema(implementation=Map.class)在Swagger ui中将模式表示为字符串
我正在尝试创建Spring boot Swagger声明schema=@schema(implementation=Map.class)在Swagger ui中将模式表示为字符串,spring-boot,swagger,springdoc,springdoc-ui,Spring Boot,Swagger,Springdoc,Springdoc Ui,我正在尝试创建springdocswagger文档,并且我希望以更好的可读方式为客户机表示具有数据类型Map的请求主体。但是当我声明@io.swagger.v3.oas.annotations.parameters.RequestBody(content=@content(schema=@schema(implementation=Map.class)时,模式将以字符串的形式出现(附在下面的屏幕截图中) 方法声明 @Operation(security = {@Security
springdoc
swagger文档,并且我希望以更好的可读方式为客户机表示具有数据类型Map
的请求主体。但是当我声明@io.swagger.v3.oas.annotations.parameters.RequestBody(content=@content(schema=@schema(implementation=Map.class)
时,模式将以字符串的形式出现(附在下面的屏幕截图中)
方法声明
@Operation(security = {@SecurityRequirement(name = "bearer-key")}, summary = "Create Data", operationId = "createData", description = "Create createData for the **`type`**. ")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Data created", content = @Content(schema = @Schema(implementation = Map.class),
examples = {@ExampleObject(value = "{\n" +
" \"id\": \"927d810c-3ac5-4584-ba58-7c11befabf54\",\n" +
"}")})),
@ApiResponse(responseCode = "400", description = "BAD Request")})
@PostMapping(value = "/data/type", produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
@io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(schema = @Schema(implementation = Map.class),
examples = {@ExampleObject(value = "{\n" +
" \"label\":\"tourism\",\n" +
" \"location\":\"France\"\n" +
" }")}))
ResponseEntity<Map<String, Object>> createData(@Parameter(name = "type", required = true) @PathVariable("type") String type, @Parameter(name = "request payload") @Valid @RequestBody Map<String, Object> body);
@Operation(security={@SecurityRequirement(name=“bearer key”)},summary=“Create Data”,operationId=“createData”,description=“Create createData for the**`type`**.”
@ApiResponses(值={
@ApiResponse(responseCode=“201”,description=“Data created”,content=@content(schema=@schema(implementation=Map.class)),
示例={@ExampleObject(value=“{\n”+
“id\:\“927d810c-3ac5-4584-ba58-7c11befabf54\,\n”+
"}")})),
@ApiResponse(responseCode=“400”,description=“坏请求”)}
@PostMapping(value=“/data/type”,products=APPLICATION\u JSON\u value,consumes=APPLICATION\u JSON\u value)
@io.swagger.v3.oas.annotations.parameters.RequestBody(内容=@content(模式=@schema(实现=Map.class)),
示例={@ExampleObject(value=“{\n”+
“标签”:“旅游业”,\n+
“\”地点\“:\”法国\“\n”+
" }")}))
ResponseEntity createData(@Parameter(name=“type”,required=true)@PathVariable(“type”)字符串类型,@Parameter(name=“request payload”)@Valid@RequestBody映射体);
尽管Spring引导会根据方法签名自动推断类型,但数据类型Map
并不清楚。例如,默认情况下,类型映射将按如下方式推断
但是我想以一种更容易理解的方式向引用我的API的客户端显示模式。我可以看到中有一个关闭的票证,没有适当的解决方案。根据我的要求,请求主体应该是一个类型不可知的动态键值对,因此除了以Map
的形式接收请求之外,没有其他方法了。ha是否有人用typeMap
实现了比创建自定义请求/响应模型更好的方法?这是springdoc openapi库的默认行为,以便忽略Spring MVC支持的其他可注入参数
如果你想改变这种行为,你可以如下所示:
SpringDocUtils.getConfig().removeRequestWrapperToIgnore(Map.class);
与大家分享我在这个问题上的工作方法,我已经为@io.swagger.v3.oas.annotations.parameters.RequestBody(content=@content(schema=@schema(implementation=Map.class)
这个模式是字符串问题
我在OpenAPI bean声明中声明了一个名为Map的自定义模式,如下所示
new OpenAPI()
.components(new Components()
.addSchemas("Map", new Schema<Map<String, Object>>().addProperties("< * >", new ObjectSchema())
))
.....
.....
@io.swagger.v3.oas.annotations.parameters.RequestBody(
content = @Content(mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(ref = "#/components/schemas/Map"))
上述方法可用于代替ApiResponse,如下所示
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200",
content = @Content(mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(ref = "#/components/schemas/Map"))
注意:如果我们使用上述自定义模式方法,我们不需要更改或忽略SpringDoc
内部正在使用的任何类型。我有一个API端点,请求主体需要一个HashMap。关于如何修复“示例值”的信息不多问题。带我到正确的地方。我发布了完整的解决方案,但所有的功劳都归他所有。(注:我试图对他的答案进行投票,但我没有足够的“分数”)
配置方面:
@配置
@OpenAPIDefinition
公共类文档配置{
@豆子
公共OpenAPI自定义OpenAPI(){
Schema newUserSchema=newschema()
.addProperties(“名称”,新StringSchema()。示例(“John123”))
.addProperties(“密码”,新StringSchema()。示例(“P4SSW0RD”))
.addProperties(“图像”,新StringSchema()。示例(“https://robohash.org/John123.png"));
返回新的OpenAPI()
//.服务器(服务器)
.info(新信息()
.title(“您的应用程序标题”)
.说明(“应用程序说明”)
.版本(“1.0”)
.license(新许可证().name(“GNU/GPL”).url(“https://www.gnu.org/licenses/gpl-3.0.html"))
)
.组件(新组件()
.addSchema(“NewUserBody”,newUserSchema)
);
}
}
控制器端:
@io.swagger.v3.oas.annotations.parameters.RequestBody(
content=@content(mediaType=mediaType.APPLICATION\u JSON\u值,
schema=@schema(ref=“#/components/schemas/NewUserBody”))
@后期映射(“/v1/users”)
公共响应upsertUser(@RequestBody HashMap user){
//你的代码在这里
}
我创建了一个HashMap扩展类:
@Schema(description = "Response-Object Map<String, EcoBalance).")
public class EcoMap extends HashMap<String, EcoBalance> {
@JsonIgnore
@Override
public boolean isEmpty() {
return super.isEmpty();
}
}
请注意,OpenAPI 3生成器不会生成这样的客户机模型,而是在OpenAPI.yml中正确引用(甚至验证)
看到问题了吗?是的,Spring MVC支持的所有可注入参数类型都被排除在外,并被大摇大摆的人忽略,完整的列表在中提供,建议的解决方案不是最优雅的,可能不适合所有的要求,主要是如果您的响应类型是Map
,那么建议的方法将不适合。谢谢!生成ApiCl由于安装时的模式映射,我必须将skipValidateSpec设置为true
@ApiResponse(responseCode = "200", content = @Content(mediaType = .., schema = @Schema(implementation = EcoMap.class)), headers = ..