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

Could not resolve pointer: /definitions/Error-ModelName

我是 Swagger.io 的新手,也是 Spring fox 的新手。我遇到的问题是,由于某种原因,一个对象没有正确引用它的模型。

UI中的错误:

错误是因为在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

希望能帮到你