Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot Spring boot REST API删除重复响应主体_Spring Boot_Rest_Maven_Swagger Ui_Springdoc Openapi Ui - Fatal编程技术网

Spring boot Spring boot REST API删除重复响应主体

Spring boot Spring boot REST API删除重复响应主体,spring-boot,rest,maven,swagger-ui,springdoc-openapi-ui,Spring Boot,Rest,Maven,Swagger Ui,Springdoc Openapi Ui,因此,我使用的是springdoc openapi ui,我能够生成可通过swagger ui访问的API 然而,我已经注意到,swagger UI中的所有响应代码都复制了200响应代码返回的响应代码,这是我没有做的,并且是非常危险的误导 如何禁用此行为? 使用Swagger2不会产生这种行为。我的方法是使用ResponseEntity返回响应。这可能是个问题吗 OpenApi30Config.java——打开文档配置文件 @配置 @OpenAPIDefinition( info=@info(

因此,我使用的是springdoc openapi ui,我能够生成可通过swagger ui访问的API

然而,我已经注意到,swagger UI中的所有响应代码都复制了200响应代码返回的响应代码,这是我没有做的,并且是非常危险的误导

如何禁用此行为? 使用Swagger2不会产生这种行为。我的方法是使用
ResponseEntity
返回响应。这可能是个问题吗

OpenApi30Config.java——打开文档配置文件

@配置
@OpenAPIDefinition(
info=@info(
title=“${spring.application.name}”,
description=“${spring.application.name}的官方API文档”,
version=“${spring.application.version}”,
联系人=@contact(name=“John Does Live”,email=“Does.John。live@stackoverflow.com"),
许可=@license(name=“Apache 2.0”)
),
security=@SecurityRequirement(name=AppConstants.API\u security\u REQUIREMENT\u name)
)
@证券计划({
@担保计划(
名称=AppConstants.API\u安全性\u要求\u名称,
in=SecuritySchemeIn.HEADER,
类型=SecuritySchemeType.APIKEY,
description=“已设置的API密钥”,
paramName=AppConstants.API_KEY_HEADER_NAME
),
@担保计划(
name=“bearerToken”,
in=SecuritySchemeIn.HEADER,
类型=SecuritySchemeType.APIKEY,
paramName=AppConstants.JWT_头_名称,
description=“用户通过服务器身份验证后获得的JWT令牌”
)
})
@简介(“开发”)
公共类OpenApi30Config{
}
GeneralAPIResponses.java——应用于所有控制器方法的注释。记录所有一般响应代码

@已记录
@目标({ElementType.METHOD,ElementType.TYPE})
@保留(RetentionPolicy.RUNTIME)
@蜂鸟反应({
@ApiResponse(responseCode=“200”,description=“操作成功”),
@ApiResponse(responseCode=“401”,description=“无效的API密钥”),
@ApiResponse(responseCode=“403”,description=“无效JWT”,内容),
@ApiResponse(responseCode=“500”,description=“内部服务器错误”)
})
public@interface GeneralAPIResponses{
}
SecuredRestapicController.java——表示端点是安全的

@SecurityRequirement(name=AppConstants.API\u SECURITY\u REQUIREMENT\u name)
公共接口安全静态控制器{
}
BaseController.java——可扩展控制器

@已验证
公共抽象类BaseController{
受保护的最终记录器日志;
受保护的BaseController(){
LOG=LoggerFactory.getLogger(getClass());
}
受保护的字符串格式(字符串格式、对象…参数){
返回String.format(Locale.US,format,args);
}
}
Foo.java

@RestController
@请求映射(“/foo”)
@标记(name=“Foo”,description=“处理所有Foo相关操作的端点”)
公共类FooController扩展BaseController实现SecuredRestaticController{
@邮戳
@操作(
description=“添加foo。”,
答复={
@ApiResponse(responseCode=“409”,description=“已经存在具有相同电话号码的Foo”)
})
@一般反应
ResponseEntity addFoo(@Valid@RequestBody FooActionRequest){
//…逻辑
返回ResponseEntity.ok(新的FooActionResponse(UUID.randomUUID(),LocalDateTime.now());
}
}
在这一切之后,我得到的是:

还有这个。(注意,
FooActionResponse
response被复制到所有响应中)我不想这样做,因为这会误导其他阅读文档的开发人员。似乎所有的状态代码都返回相同的内容,而API却没有


发生此问题是因为您没有为每个附加响应指定
@Content
。SpringDoc假定响应主体与带注释的方法相同

如果您有用于错误响应的通用错误模型,则可以在“内容”部分中指定该模型:

    @ApiResponses({
        @ApiResponse(responseCode = "401", description = "Invalid API key", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ErrorModel.class))),
        @ApiResponse(responseCode = "403", description = "Invalid JWT", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ErrorModel.class))),
        @ApiResponse(responseCode = "500", description = "Internal server error", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ErrorModel.class))),
    })
如果只需要错误描述,而不需要任何架构,则需要为响应指定空的
@Content

    @ApiResponses({
        @ApiResponse(responseCode = "401", description = "Invalid API key", content = @Content),
        @ApiResponse(responseCode = "403", description = "Invalid JWT", content = @Content),
        @ApiResponse(responseCode = "500", description = "Internal server error", content = @Content),
    })

请添加控制器方法的示例。这可能是由于您在上面使用的注释造成的。@VadymVL我添加了更多的脱轨。请查收。谢谢,谢谢。就这样。尽管我想知道是否可以配置此行为,以避免推断响应的内容。