当前位置:网站首页>Swagger use

Swagger use

2022-06-30 08:14:00 sashaSloan

swagger Use

Novice notes , Crazy Porter
** This chapter is based on the optimization part of the crazy God Ruiji takeout project

Madness theory Java

Introduce

Use Swagger You just need to define the interface and the interface related information according to its specifications , Re pass Swagger A series of derived projects and tools , You can generate various formats of interface documents , And online interface debugging page and so on .
Official website : https:/ /swagger.io/

knife4j Is for Java MVC Framework for the integration Swagger Generate Api Document enhancement solution .

swagger Use

swagger To configure

1、 Import maven coordinate

<dependency>
<groupld>com.github.xiaoymin</groupld>
<artifactld>knife4j-spring-boot-starter</artifactld>
<version>3.0.2</version>
</dependency>

2、 Import knife4j Related configuration classes (WebMvcConfig)
stay mvc The configuration file WebMvcConfig In the open swagger and knife4j

@EnableSwagger2
@EnableKnife4j
public class WebMvcConfig extends WebMvcConfigurationSupport {
......
// Create the following two methods in the class 
    @Bean
    public Docket createRestApi()
    {
        // The document type 
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.itheima.reggie.controller"))
                .paths(PathSelectors.any())
                .build();
    }
        private ApiInfo apiInfo(){
            return new ApiInfoBuilder()
                    .title(" Reggie takeout "). version("1.0")
                    . description(" Reggie takeout interface document ").build() ;
        }

3、 Set up static resources ( Otherwise, the interface document page cannot be accessed )
Set static resource mapping (WebMvcConfig Class addResourceHandlers Method ), Otherwise, the interface document page cannot be accessed .

        //  solve swagger cannot access 
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        //  solve swagger Of js File not accessible 
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

4、 stay LoginCheckFilter Set the request path that does not need to be processed in
 Insert picture description here
visit localhost:8080/doc.html
 Insert picture description here
Here you can see the interface information and test the interface .

swagger Commonly used annotations

 Insert picture description here Add relevant annotations to classes and methods for configuration
 Insert picture description here

 Insert picture description here
 Insert picture description here

原网站

版权声明
本文为[sashaSloan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/181/202206300804256829.html