简而言之Springboot Swagger

时间:2019-01-03 12:41:28

标签: spring-boot spring-mvc swagger

在实施Swagger时,我从社区/博客中获得了很多帮助,但是却只有一个地方。所以我总结了关键的一些,只是为了节省其他人的时间。

1。需要包括依赖项。

        <!-- Added for Swagger Dependency -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.8.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.8.0</version>
        <scope>compile</scope>
    </dependency>
    <!--END Added for Swagger Dependency -->
  1. 您需要具有Swagger配置类..

    @配置 @ EnableSwagger2 公共类SwaggerConfig扩展了WebMvcConfigurationSupport {

    @Bean
    public Docket productApi() {
    
    
        /*   Swagger : For Particular Rest Service under somepackge, someEndpoint
    
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("com.abcdli.somepackge.controller"))
                .paths(PathSelectors.regex("/someEndpoint.*")).build();*/
    
    
        /*  Swagger : Simple method for all the apis    
    
          return new Docket(DocumentationType.SWAGGER_2).select()
          .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build();
         */
    
         /*  
         Swagger : Simple method for all the apis under somepackage only         
         */
    
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("com.abcdli.yourpackage.controller"))
                .paths(PathSelectors.any())
                .build()
                 /*  
                 Swagger : metaInfo() is used for enrichment with details 
                    ApiInfo(title, description, version, termsOfServiceUrl, contactName, license, licenseUrl);
                 */
                .apiInfo(metaInfo());
    
    
    
    }
    
    private ApiInfo metaInfo() {
    
        String description = ;
    
    
        return new ApiInfoBuilder()
                .title("YOUR APPLICATION NAME")
                .description(description)
                .version("1.0.0.1")
                .contact(new springfox.documentation.service.Contact("TEAM","www.mw.com","mw.abcd.com"))
                .build();
    
    
    }
    
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-
    ui.html").addResourceLocations("classpath:/META-INF/resources/");
    
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
    

    }

3。现在,Swagger页面已准备就绪。现在需要自定义控制器,方法和模型。这很简单。

/*
Registering the RestAPI
*/
@Api(value="YourController",description="some description")
public class YourController {
    @RequestMapping(value = "/path", method = RequestMethod.POST, 
consumes = {
            "application/JSON" }, produces = { "application/JSON" })

/*
Registering the method
*/          
    @ApiOperation(value="Adding a method",response=Response.class)
    public ResponseEntity<Response> addMethodDetails(
            @RequestBody modifyReq) {

}
  1. 如果您的API通过某种身份验证得到保护,则需要提供对这些资源的访问权限。

    • // http.anonymous()。disable(); [匿名]

    • http.authorizeRequests()     .antMatchers(“ / swagger-resources / ”,             “ swagger-resources / configuration / ui / *”,             “ /swagger-ui.html ”,             “ / webjars / **”,             “ favicon.ico”)。permitAll();

0 个答案:

没有答案