美文网首页
spring boot 配置swagger2

spring boot 配置swagger2

作者: 疯狂的冰块 | 来源:发表于2017-06-24 20:31 被阅读1244次

引入pom文件依赖:

      <!-- swagger -->
        <!-- 文档自动生成 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.4.0</version>
        </dependency>

配置文件:

@Configuration
@EnableSwagger2
@ComponentScan("com.hexun.bdc.auth")
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.hexun.bdc.auth.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("CashBack's RESTful APIs")
                .description("更多请关注:http://localhsot/")
                .termsOfServiceUrl("http://bdcresttest.hexun.com/cashback2server")
                .version("1.0")
                .build();
    }
}

在浏览器中输入即可访问了:
http://localhost:8010/swagger-ui.html

swagger使用:
1、 接口注释@ApiOperation(value="获取用户列表", notes="")


image.png

2、类注解:@Api

用在类上,说明该类的作用

比如说:@Api(value = “UserController”, description = “用户相关api”)

@Api(value = "MaterialController ",description = "用户相关接口")
@Controller
@RequestMapping("user")
public class UserController {
    
}
 

参考文章:
http://www.souvc.com/?p=2869

相关文章

网友评论

      本文标题:spring boot 配置swagger2

      本文链接:https://www.haomeiwen.com/subject/ikbhcxtx.html