如何解决无法解析指针:/ definitions / Error-ModelName
我对Swagger.io和Spring Fox还是陌生的。我遇到的问题是,由于某种原因,一个对象未正确引用其模型。
错误是因为它最终像这样在JSON中出现:
"schema": {
"$ref": "#/definitions/Error-ModelName{namespace='online.staffmanager.backend.auth.model.dto',name='UserChangeSet'}"
}
如果我将其更改为:
"schema": {
"$ref": "#/definitions/UserChangeSet"
}
它确实起作用。而且我不知道为什么注释会这样映射它。
我的注释:
@Operation(
tags = "auth",summary = "Create a new User Account",responses = {
@ApiResponse(
responseCode = "200",content = @Content(schema = @Schema(implementation = TokenInfo.class))),@ApiResponse(
responseCode = "201",content = @Content(schema = @Schema(implementation = UserChangeSet.class)))
}
)
SpringFoxConfig:
@Configuration
@EnableSwagger2
@EnableWebMvc
public class SpringFoxConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
注意:我正在使用Springfox 3.0.0。 预先感谢!
解决方法
您必须再向 Bean 添加一项配置。
这里是需要添加的配置:
.additionalModels(
typeResolver.resolve(TokenInfo.class),typeResolver.resolve(UserChangeSet.class)
)
这是完整的代码:
@Configuration
@Import(SpringDataRestConfiguration.class)
public class SwaggerUIConfig {
@Bean
public Docket api(TypeResolver typeResolver) {
return new Docket(DocumentationType.SWAGGER_2)
.additionalModels(
typeResolver.resolve(TokenInfo.class),typeResolver.resolve(UserChangeSet.class)
)
.select()
.apis(RequestHandlerSelectors.basePackage("com.projectname.controllers"))
.paths(PathSelectors.any())
.build()
.useDefaultResponseMessages(false);
}
}
注意:@EnableSwagger2 注解建议在 3.0 版本中移除。您可以参考http://springfox.github.io/springfox/docs/current/#migrating-from-existing-2-x-version
希望能帮到你。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。