博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring boot集成swagger2
阅读量:7093 次
发布时间:2019-06-28

本文共 3059 字,大约阅读时间需要 10 分钟。

2019年4月4日15:00:30

 

pom添加更新

io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2

 

添加注册配置文件

@EnableSwagger2@Configurationpublic class SwaggerConfig {    // 是否开启swagger,正式环境一般是需要关闭的    @Value(value = "${swagger.enabled}")    Boolean swaggerEnabled;    @Bean    public Docket createRestApi() {        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(swaggerEnabled).select()                .apis(RequestHandlerSelectors.basePackage("com.zs.logistics")).paths(PathSelectors.any()).build();    }    private ApiInfo apiInfo() {        return new ApiInfoBuilder().title("API文档").version("1.0.0").build();    }}

application.properties配置

swagger.enabled=true

线上的时候关闭

RequestHandlerSelectors.basePackage("com.zs.logistics")

这个是你需要扫描的包的路径,根据需求配置

 

注意这个光这个是不够的,还要配置swagger-ui

 

@Configurationpublic class AppConfig extends WebMvcConfigurationSupport {    @Autowired    private AdminLoginInterceptor adminLoginInterceptor;    public static String[] adminAllowUrl = { "/api/admin/login" };    @Override    public void addResourceHandlers(ResourceHandlerRegistry registry) {        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");        registry.addResourceHandler("/upload/**").addResourceLocations("classpath:/upload/");        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");    }

 

接口里面使用swagger注解

@Api(tags = "管理员操作相关接口")@RestControllerpublic class LoginApiController {    @Autowired    private AdminService adminService;    @ApiOperation(value = "管理员登录接口")    @PostMapping("/api/admin/login")    public Map
admin(@RequestParam(value = "name", defaultValue = "") String name, @RequestParam(value = "password", defaultValue = "") String password) { try {return ResponseHelper.responseMenu(200, "成功", admin, adminPermission); } catch (Exception e) {// return ResponseHelper.responseMessage(201, e.getMessage() + e.toString()); return ResponseHelper.responseMessage(201, e.getMessage()); } }}

 

访问localhost:8080/swagger-ui.html

其他常用注解

- @Api()用于类; 表示标识这个类是swagger的资源 - @ApiOperation()用于方法; 表示一个http请求的操作 - @ApiParam()用于方法,参数,字段说明; 表示对参数的添加元数据(说明或是否必填等) - @ApiModel()用于类 表示对类进行说明,用于参数用实体类接收 - @ApiModelProperty()用于方法,字段 表示对model属性的说明或者数据操作更改 - @ApiIgnore()用于类,方法,方法参数 表示这个方法或者类被忽略 - @ApiImplicitParam() 用于方法 表示单独的请求参数 - @ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam

详细使用参考

https://www.cnblogs.com/fengli9998/p/7921601.html

 

当你需要废弃某个方法的时候,加上

@Deprecated

swagger2生成的文档也会是灰色

转载于:https://www.cnblogs.com/zx-admin/p/10655004.html

你可能感兴趣的文章
I/O重定向和管道——《Unix/Linux编程实践教程》读书笔记(第10章)
查看>>
华章1-2月份新书简介(2018年)
查看>>
PreparedStatement的用法
查看>>
For多重循环 break continue
查看>>
Spring源码解析:Bean实例的创建与初始化
查看>>
我的友情链接
查看>>
百度是如何给每个人免费提供2TB存储空间的?
查看>>
php7.2.6源码编译安装
查看>>
Titanium Terminal 不好使的解决办法
查看>>
我的友情链接
查看>>
BaseRecyclerViewAdapterHelper开源项目之点击事件源码学习
查看>>
JAVA解析JSON大全
查看>>
Java基础学习总结(23)——GUI编程
查看>>
让App的运行速度与响应速度趋于一流(iOS)
查看>>
大型网站技术架构(八)网站的安全架构
查看>>
Java基础学习总结(16)——Java制作证书的工具keytool用法总结
查看>>
学习Redis必须了解的N个常识
查看>>
源码配置svn+apache
查看>>
找出数组中唯一重复的数
查看>>
JAVA中sleep()、wait()、yield()、join()方法浅析
查看>>