是否可以通过名称空间动态创建标准规范?

时间:2019-09-01 00:57:09

标签: asp.net-core swagger

我想创建swagger规范,并按我的mvc应用程序中的命名空间分组。

例如,如果我具有以下api控制器:

- Portal.Example1.Sub1
- Portal.Example1.Sub2
- Portal.Example2.Section2
- Portal.Example2.Section4
- Portal.Example3.Something4
- Portal.Example3.Whatever69

我希望输出swagger-ui页面为Example1创建一个规范,其中包含Sub1Sub2中的所有操作,等等...

当前,我的代码设置如下:

Startup:ConfigureServices中:

services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("v1", new Info { Title = "Portal API", Version = "v1" });
    options.DocInclusionPredicate((docName, description) => true);
    options.EnableAnnotations();
});

Startup:Configure

app.UseSwagger();

app.UseSwaggerUI(options =>
{
    options.SwaggerEndpoint("/swagger/v1/swagger.json", "Portal API V1");
    options.IndexStream = () => Assembly.GetExecutingAssembly().GetManifestResourceStream("Portal.Web.wwwroot.swagger.ui.index.html");
    options.InjectBaseUrl(""http://localhost:62114/");
}); //URL: /swagger

1 个答案:

答案 0 :(得分:0)

我认为我已经在春季创建了此类课程:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(value = "categoryA", description = "categoryA Managent")
@RestController
@RequestMapping("/api/categoryA")
public class CategoryA {
...
}

当我查看代码时,它与一个软件包一起工作。如果遇到问题,则可能需要按照以下方式进行更改:

@SpringBootApplication
@EnableSwagger2
@ComponentScan(basePackages = {"Portal.controller", "Portal.AllExamples"}) 
public class Application {

    @Value("${info.app.version:unknown}") String version;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage(Portal.AllExamples.class.getPackage().getName()))
                .paths(PathSelectors.regex("/.*")).build().apiInfo(apiEndPointsInfo());
    }

    public ApiInfo apiEndPointsInfo() {
        return new ApiInfoBuilder().title("My REST API").description("bla bla bla.")
                .contact(new Contact("yourcorp", "yoururl", "you@mail"))
                .license("free for all license").licenseUrl("https://free.beer.org").version(version)
                .build();
    }

...