springboot integrates swagger2 and lombok

Step 1: Create a maven project

Then introduce the required jars into our project:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- tomcat的支持. -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
    </dependency>
    <!-- mybatis -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!-- swagger2 -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.7.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.7.0</version>
    </dependency>
    <!-- lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>1.5.8.RELEASE</version>
    </dependency>
</dependencies>

We need to add the lombok plugin to our idea:

Settings->plugins Search for lombok and install it directly after finding it

Okay, let’s go straight to the code:

springboot startup class:

package com.swa;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@MapperScan(basePackages="com.sw.mapper")
@ComponentScan(basePackages={"com.swa.*"})
@EnableSwagger2
@Configuration
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

swagger configuration class:

package com.swa;

import springfox.documentation.service.Contact;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class SwaggerConfig {
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) 
                .select()// Current package 
                path.apis(RequestHandlerSelectors.basePackage("com.swa.controller")) 
                .paths(PathSelectors.any()). build(); 
    } 
    //Build the detailed information function of the api document 
    private ApiInfo apiInfo(){ 
        return new ApiInfoBuilder() //Page title.title 
                ("springBoot test uses Swagger2 to build RESTful API") 
                //Creator.contact(new Contact("chendai","http://www.baidu.com","")) //Version number.version 
                ("1.0") //Description 
                .description("API description") 
                .build(); 
    } 
} 
Controller for testing:
package com.swa.controller;

import com.swa.model.StuInfo;
import com.swa.service.StuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "/homework", description = "学生作业接口", tags = "TestController")
@Slf4j
@RequestMapping(value = "/homework", produces = "application/json;charset=utf-8")
@RestController
public class TestController {

    @Autowired
    private StuService stuService; 
    
    @ApiOperation(value = "Get student basic information", notes = "Get student basic information") 
    @GetMapping("/getStuInfo") 
    public StuInfo getStuInfo() { 
        StuInfo stuInfo = new StuInfo(); 
        stuInfo.setStuId (1000L); 
        stuInfo.setStuName("Brady"); 
        return stuInfo; 
    } 
}

model class:

package com.swa.model;

import io.swagger.annotations.ApiModel;
import lombok.Data;

@ApiModel
@Data
public class StuInfo {

    private Long stuId;

    private String stuName;
}

Okay, done, direct access:

http://localhost:8080/swagger-ui.html 

This address is fixed, be sure to remember it

As for the other configurations of connection data, I won’t go into details here. If you don’t know, please check the springboot blog I wrote before.

If you have any questions, you can comment below and make progress together.

 

 

Guess you like

Origin blog.csdn.net/Brady74/article/details/82960028