无法解析指针:/ definitions / Error-ModelName

问题描述

我对Swagger.io和Spring Fox还是陌生的。我遇到的问题是,由于某种原因,一个对象未​​正确引用其模型。

UI中的错误:

enter image description here

错误是因为它最终像这样在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

希望能帮到你。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...