本篇文章为大家展示了springboot 中配置使用swagger2的方法,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
1. maven依赖包
使用目前最新版本为例,pom.xml添加的代码如下
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
2. 配置类的编写
配置类的编写同样非常简单,可以直接复制粘贴以下代码,但是一定要注意做适当修改,尤其是设置basePackage的路径,一定要根据实际情况修改。
新建一个config文件夹,在此文件夹中新建一个类
package cn.smileyan.swagger.config;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
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;
@EnableSwagger2
@Configurable
public class Swagger2 {
/**
* 特别要注意.apis(RequestHandlerSelectors.basePackage("cn.smileyan.swagger.controller"))
* 此中的cn.smileyan.swagger.controller一定要修改为自己controller包。
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.smileyan.swagger.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("springboot使用swagger例子")
.description("简单优雅的restful风格")
.termsOfServiceUrl("https://smileyan.cn")
.version("1.0")
.build();
}
}
不能忘记类前面的@EnableSwagger2 与 @Configurable配置注解。以及后面的@Bean注解。
3. @EnableSwagger2 不能忘了
除了这个位置需要添加这个注解,还有springboot的运行类(application类)也要添加这个注释,否则会出现错误。
如图所示,我的application类名为SwaggerApplication,在这个类上面添加@EnableSwagger2
package cn.smileyan.swagger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class, args);
}
}
4. 编写controller类,添加注解,注意这个controller路径与上面配置类的路径要保持一致。
package cn.smileyan.swagger.controller;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/user")
public class UserController {
@ApiOperation(value = "用户测试",notes = "贵宾用户")
@RequestMapping(value = "",method = RequestMethod.GET)
private Map<String,String> getUser() {
Map<String,String> map = new HashMap<>(1);
map.put("result","success");
return map;
}
}
5. 运行,打开api文档http://localhost:8080/swagger-ui.html
效果如下:
可以点开user-controller,效果如下:
常用注解
@Api : 修饰整个类,用于描述Controller类
@ApiOperation:描述类的方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应的一个描述
@ApiResponses:HTTP响应的整体描述
@ApiIgnore:使用该注解,表示Swagger2忽略这个API
@ApiError:发生错误返回的信息
@ApiParamImplicit:一个请求参数
@ApiParamsImplicit:多个请求参数
上述内容就是springboot 中配置使用swagger2的方法,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。