SpringBoot integration Swagger

This is my first article on getting started


preface

When Web back-end people develop interfaces, they need to test them, which is the tool we can use today, Swagger.

What is Swagger?

  • Swagger website url: API Documentation & Design Tools for Teams | Swagger

Go to the official Swagger website to see the official introduction

Simplify API development for users, teams, and enterprises with the Swagger open source and professional toolset. Find out how Swagger can help you design and document your APIs at scale.

In short, it is a tool to help users simplify API development by visualizing the API for developers to design and debug.

Since I mainly use Java, today I want to introduce how to integrate Swagger in SpringBoot.

Create a New SpringBoot project and import dependencies

  • Introduce Swagger’s dependency into the SpringBoot project

    <! -- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
    <dependency>
    	<groupId>io.springfox</groupId>
    	<artifactId>springfox-boot-starter</artifactId>
    	<version>3.0.0</version>
    </dependency>
    Copy the code
  • The project structure is shown in figure

Create a Swagger configuration class

The position of Swagger configuration class is shown in the figure above

The Swagger configuration class configures information about Swagger scanning interfaces and authors

The configuration class code is as follows

/ * * *@author xunle
 */
@EnableSwagger2
@Configuration
public class SwaggerConfig {
    /** * Configures Swagger information */
    private ApiInfo getApiInfo(a) {
        // Author information
        Contact contact = new Contact("xunle"."https://xunle1.github.io/"."[email protected]");
        return new ApiInfoBuilder()
                / / title
                .title("Swagger Test")
                // Description
                .description("Swagger SpringBoot integration")
                / / version number
                .version("v1.0")
                // Author information
                .contact(contact)
                .build();
    }

    /** * Used to configure Swagger scan interface */
    @Bean
    public Docket docket(a) {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getApiInfo())
                .select()
                //apis are used to configure which interfaces to scan
                .apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
                //paths Is used to configure path selection
                .paths(PathSelectors.ant("/hello/**"))
                .build()
                // Whether to enable Swagger
                .enable(true); }}Copy the code

The new Controller

The Controller location is shown in the figure above

The Controller class defines interfaces

/ * * *@author xunle
 */
/ / Api class information
@API (tags = "Swagger test Controller")
@Controller
public class SwaggerController {
    //ApiOperation method description
    @apioperation ("Swagger test Get method ")
    @ResponseBody
    @GetMapping("/hello/test")
    public String test(a) {
        return "Swagger test";
    }
    
    @apiOperation ("Swagger scan path ")
    @ResponseBody
    @GetMapping("/hi/test")
    public String testPathSelector(a) {
        return "selected!"; }}Copy the code

Swagger- UI Basic information

Normally access project name /swagger-ui.html

The author native to http://localhost:8080/swagger-ui.html

The configuration of the first two steps is displayed in Swagger- UI. You can check the corresponding display of the configuration

/hi/test does not match /hello/** in the Swagger configuration class, so it is not displayed in Swagger UI

The interface test

After clicking on the corresponding API

  • Parameters indicates a parameter list
  • Response is returned information
    • Code: indicates the status Code
    • Description: If the access succeeds, the data type is displayed in Example Value and the entity class is returned in Model

Click Try it Out and the following will appear:

Click Execute to perform the interface test, and the following output is displayed:

  • Response Body: The return message is the return value of our first method definition
  • Response headers: Indicates the Response headers

Write in the last

This concludes SpringBoot integration with Swagger and testing interfaces in Swagger-UI. This is the first time for me to write this article, and I have to concentrate on reviewing for the final exam in these two days (if I don’t work hard at ordinary times, I will be lucky at the end of the exam 😭), so Swagger still has a lot to say, including Swagger’s annotation description of butt mouth parameters, description of entity classes, etc., which will be filled up after the final exam. If any comrades find any errata, please contact me for correction. Finally, I wrote in a hurry, and there are many related articles on the Internet. If someone would like to stay and watch or even give a thumb-up, that would be a great encouragement to me. Thank you for watching.