SpringBoot integrated Swagger2 steps

 1, adding a dependency: the first to add Swagger2 dependency in pom.xml:

<!-- swagger依赖 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>
<!-- 用于生成在线文档的 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

2, description of the controller class:

  (1), to create a simple entity class to store data:

public class Book {
    //属性
    private Integer bid;
    private String bname;
    private String bprotagonist;
    private String btype;
    
    public Book() {
        super();
    }
    public Book(Integer bid, String bname, String bprotagonist, String btype) {
        super();
        this.bid = bid;
        this.bname = bname;
        this.bprotagonist = bprotagonist;
        this.btype = btype;
    }
    public Integer getBid() {
        return bid;
    }
    public void setBid(Integer bid) {
        this.bid = bid;
    }
    public String getBname() {
        return bname;
    }
    public void setBname(String bname) {
        this.bname = bname;
    }
    public String getBprotagonist() {
        return bprotagonist;
    }
    public void setBprotagonist(String bprotagonist) {
        this.bprotagonist = bprotagonist;
    }
    public String getBtype() {
        return btype;
    }
    public void setBtype(String btype) {
        this.btype = btype;
    }
    @Override
    public String toString() {
        return "Book [bid=" + bid + ", bname=" + bname + ", bprotagonist=" + bprotagonist + ", btype=" + btype + "]";
    }
}

  (2) create a simple controller to simulate data showing the effect of:

/ ** 
 * controller class 
 * / 
@RestController 
public  class BookController {
     / ** 
     * Get all the data 
     * / 
    @GetMapping ( "/ GET" )
     public List <Book> getBook () { 
        List <Book> Books = new new the ArrayList < > (); 
        books.add ( new new Book (. 1, "garden baby", "wow card", "cartoon" )); 
        books.add ( new new Book (2, "broken sky", "Xiao-yan", " Fiction " )); 
        books.add ( new new Book (1," Qingyu years "," free range "," novel " ));
        return books;
    }
    /**
     * 添加
     * @param book
     * @return
     */
    @GetMapping("/add")
    public Book addBook(Book book) {
        return book;
    }
    
}

3, prepared Swagger2 configuration class, the scanning controller SpringMVC:

  Most of the configuration code in the class is fixed, but note that the controller can not be wrong package name, are copied recommended CTRL + SHITF + O {} shortcut automatically introduce the required packages:

/ ** 
 * Swagger2 configuration class 
 * / 
@Configuration     // denoted as configuration class 
@ EnableSwagger2 // for activating Swagger2 App also written to the boot class 
public  class Swagger2Config { 
    
    @Bean 
    public Docket No. createRestApi () {
         // scan controller in Swagger2 notes, .apis inside Indicates that the specified package 
        return  new new Docket (DocumentationType.SWAGGER_2) 
                .apiInfo (apiInfo ()) 
                .Select ()                             // package name of the controller                 
                .apis (RequestHandlerSelectors.basePackage ( "com.szc .controller " )) 
                .paths (PathSelectors.any ())
                .build (); 
    } 

    // API descriptions 
    Private ApiInfo apiInfo () {
         return  new new ApiInfoBuilder () 
                .title ( "SprignBoot integration sawgger2 online Api Documentation")   // title 
                .description ( "information describing the document")   // Description 
                .termsOfServiceUrl ( "https://baidu.com")   // URL 
                .version ( "szc_1.0")   // version 
                .build (); 
    } 

}

4, start SpringBoot project in App startup class:

5, after the successful launch, in the browser address bar, enter HTTP: // localhost: 8080 / Swagger-ui.html , you can view the

 Opening the controller name, you can see which way the

 Opening the layer by layer method, a method can also view the desired parameters or return data

 

Guess you like

Origin www.cnblogs.com/szcy/p/12509494.html