SpringBoot learning (three) - springboot rapid integration of swagger document

@ [Heel]

Brief introduction

advantage

According to the back-end swagger grammar, automatically generates pretty standard interface documentation.

Do interactive tests.

Disadvantaged

Intrusive, affect the program running, especially when parameter passing.

note

swagger 1.2 and 2.0 points, quite different. swagger1.2 i.e. swagger-ui; swagger2.0 i.e. springfox-swagger. This article describes the use of a new version, springfox-swagger.

Posted production, close swagger, to prevent leakage interface documentation project, was ***

The introduction of swagger components

pom.xml added

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

Code combat

I see a lot of bloggers say swagger and configuration code to start the project files in the same directory that follows

Here Insert Picture Description

However, moved into config directory, after testing, it is normal that this depends on personal habits.

DemoApplication.java

package com.example;

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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

//通过 @Configuration 注解,让 Spring 来加载该类配置。
//再通过 @EnableSwagger2 注解来启用 Swagger2。
@Configuration
@EnableSwagger2
public class DemoSwagger {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 指定要扫描的包路径
             .apis(RequestHandlerSelectors.basePackage("com.example.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("项目api文档")
                .description("swagger接入教程")
                .version("1.0")
                .build();
    }
}

Because before been configured spring security, so the browser, enter the URL http: // localhost: 8080 / swagger -ui.html later, will be blocked to live, a good user configuration before you enter the password, the effect is as follows;

Here Insert Picture Description

Because the test before the user login, user rights, so there have been some controller interface methods, but you let it default, obviously the user experience is not good, so in the previous years continued to add comment userController of swagger.

@Api:用在类上,说明该类的作用。

@ApiOperation:说明该方法的作用。

Specific and more detailed notes refer to the official documentation used explanatory notes .

UserController.java

package com.example.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("user")
@Api(value = "用户模块说明", description = "提供用户的增、删、改、查")
public class UserController {

    @RequestMapping(value = "/addUser", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "添加用户", notes = "放一些信息,供测试判断")
    String addUser() {
        return "这是添加用户!!!";
    }

    @RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除用户", notes = "放一些信息,供测试判断")
    String deleteUser() {
        return "这是删除用户!!!";
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    @ApiOperation(value = "修改用户", notes = "放一些信息,供测试判断")
    String updateUser() {
        return "这是修改用户!!!";
    }

    @RequestMapping(value = "/findAllUsers", method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value = "查询用户", notes = "放一些信息,供测试判断")
    String findAllUsers() {
        return "这是查询用户!!!";
    }

}

FIG effect following
Here Insert Picture Description
Here Insert Picture Description
specific open a one, the following

Here Insert Picture Description

Obviously, with Chinese notes, documents more readable.

It is noted that, @RequestMapping usually write notes, I usually abbreviated, modify the user of the method as demo. But the swagger is intrusive, if RequestMethod type is not specified, it will put a lot are listed, such as GET, HEAD, POST, PUT, DELETE, OPTIONS, PATCH, and other designated good, it is a.

Guess you like

Origin blog.51cto.com/14089205/2452128