Swagger2配置

配置类

package top.yalong;

import org.springframework.beans.factory.annotation.Value;
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;

/**
 * Swagger2 配置
 *
 * @author liuyalong
 */
@Configuration
@EnableSwagger2
public class Swagger2 {

    @Value("${swagger.show}")
    private boolean swaggerShow;

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Lyl's API")
                // 开发人员信息
                //.contact(new Contact("Yalong Liu","http://www.yalong.top","4379711@qq.com"))
                .version("1.0")
                //.description("刘亚龙的接口文档")
                .build();
    }

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerShow)
                .apiInfo(apiInfo())
                .groupName("用户接口")
                .select()
                // 可以扫描指定的某个包
                // .apis(RequestHandlerSelectors.any())
                .apis(RequestHandlerSelectors.basePackage("top.yalong"))
                .paths(PathSelectors.any())
                .build();

    }
}

pom

 <!--   swagger     -->
        <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>

yaml设置属性是否开启

swagger:
    show: true

controller使用示例

package top.yalong.web.user;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.yalong.pojo.user.User;
import top.yalong.service.user.UserService;
import top.yalong.common.error.MyException;

import java.util.List;


/**
 * @author liuyalong
 */
@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "查询用户", notes = "根据id来查询用户")
    @GetMapping("/query/{id}")
    public List<User> query(@PathVariable int id) {
        if (id == 0) {
            throw new RuntimeException("我自己抛出一个全局异常");
        } else if (id == 1) {
            throw new MyException(9523, "我自己抛出一个异常");
        }

        return userService.query();
    }

}
posted @ 2020-12-09 15:37  rm-rf*  阅读(76)  评论(0编辑  收藏  举报