MENU

SpringBoot整合Swagger2

概要

SpringBoot整合Swagger2,一个很方便的自动生成文档的

步骤

先上依赖
pom.xml

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

接下来写一个配置类
SwaggerConfig

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.wanvale.pms.controller"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .title("标题")
                        .description("详细说明")
                        .version("9.0")
                        .contact(new Contact("网站名", "https://网址", "邮箱@youxiang.com"))
                        .license("GPLv3 开源协议")
                        .licenseUrl("http://www.gnu.org/licenses/gpl-3.0.html")
                        .build());
    }
}

如果你用了shiro,记得修改配置

        map.put("/swagger-ui.html","anon");
        map.put("/swagger-resources","anon");
        map.put("/swagger-resources/configuration/security","anon");
        map.put("/swagger-resources/configuration/ui","anon");
        map.put("/v2/api-docs","anon");
        map.put("/webjars/springfox-swagger-ui/**","anon");

接下来给Controller里写几个注解试一下

@ApiOperation("用户登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="username",value="用户名",defaultValue = "",required = true),
            @ApiImplicitParam(name="password",value="密码",defaultValue = "",required = true)
    })
    @RequestMapping(value="/login",method = RequestMethod.POST)
    public String login(User user){
...

运行,访问/swagger-ui.html试一下
1.jpg
点一下Try it out,输入要提交的字段value,再点execute就可以直接提交数据测试了
是不是很方便呢


文章标题:SpringBoot整合Swagger2
如果文中内容侵犯了您的权益,请及时与博主取得联系进行删除!
本站文章未经许可禁止转载,本文地址:https://blog.wanvale.com/archives/106/

Last Modified: June 20, 2020
Archives QR Code Tip
QR Code for this page
Tipping QR Code