SpringBoot整合Swagger2
1,Swagger2简介2,使用SpringBoot整合Swagger2步骤1,导入依赖2,写一个Swagger2的配置工具类3,在项目启动类中,声明swagger2的使用4,修饰类和方法,以及方法中的参数一个参数多个参数
1,Swagger2简介
在java程序中,帮助建立接口测试功能,生成接口文档用于前后端分离的团队协作开发 后台程序员可以借助这个工具,专心写后台逻辑,还能保证前端可以实时的准确的参与合作
2,使用SpringBoot整合Swagger2步骤
1,导入依赖
<dependency>
<groupId>io.springfox
</groupId>
<artifactId>springfox-swagger-ui
</artifactId>
<version>2.9.2
</version>
</dependency>
<dependency>
<groupId>io.springfox
</groupId>
<artifactId>springfox-swagger2
</artifactId>
<version>2.9.2
</version>
</dependency>
2,写一个Swagger2的配置工具类
package com
.azapp
.demo
.swaggertest
.utils
;
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
.service
.Contact
;
import springfox
.documentation
.spi
.DocumentationType
;
import springfox
.documentation
.spring
.web
.plugins
.Docket
;
import springfox
.documentation
.swagger2
.annotations
.EnableSwagger2
;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket
docket(){
return new Docket(DocumentationType
.SWAGGER_2
).apiInfo(apiInfo())
.select().apis(RequestHandlerSelectors
.
basePackage("com.azapp.demo.swaggertest.controller"))
.paths(PathSelectors
.any()).build();
}
private ApiInfo
apiInfo(){
return new ApiInfoBuilder().title("CRUD项目的后台接口")
.contact(new Contact("chengweisheng","https://blog.csdn.net/weixin_45557544",
"1578753145@qq.com"))
.version("1.0.1").description("第一个接口文档")
.build();
}
}
3,在项目启动类中,声明swagger2的使用
package com
.azapp
.demo
.swaggertest
;
import org
.mybatis
.spring
.annotation
.MapperScan
;
import org
.springframework
.boot
.SpringApplication
;
import org
.springframework
.boot
.autoconfigure
.SpringBootApplication
;
import springfox
.documentation
.swagger2
.annotations
.EnableSwagger2
;
@SpringBootApplication
@EnableSwagger2
@MapperScan(basePackages
= "com.azapp.demo.swaggertest.dao")
public class SwaggertestApplication {
public static void main(String
[] args
) {
SpringApplication
.run(SwaggertestApplication
.class, args
);
}
}
4,修饰类和方法,以及方法中的参数
一个参数
package com
.azapp
.demo
.swaggertest
.controller
;
import io
.swagger
.annotations
.*
;
import org
.springframework
.web
.bind
.annotation
.GetMapping
;
import org
.springframework
.web
.bind
.annotation
.RestController
;
@RestController
@Api(tags
= "这是TestController中的内容")
public class TestController {
@ApiOperation(value
= "这是打招呼的方法")
@GetMapping("/hello")
public String
hello(){
return "hello";
}
@ApiOperation(value
= "这是鉴别姓名的方法")
@ApiImplicitParams({
@ApiImplicitParam(name
= "name",example
= "aaa",required
= true)
})
@ApiResponses({
@ApiResponse(code
= 501,message
= "数据库操作错误"),
@ApiResponse(code
= 502,message
= "Java操作错误"),
@ApiResponse(code
= 503,message
= "Redis操作错误")
})
@GetMapping("/checkName")
public String
checkName(String name
){
if (name
.equals("admin")){
return "hello admin";
}else{
return "wrong name";
}
}
}
多个参数
package com
.azapp
.demo
.swaggertest
.controller
;
import com
.azapp
.demo
.swaggertest
.entity
.ReturnResult
;
import com
.azapp
.demo
.swaggertest
.entity
.Student
;
import com
.azapp
.demo
.swaggertest
.service
.StudentService
;
import com
.azapp
.demo
.swaggertest
.utils
.ReturnUtils
;
import io
.swagger
.annotations
.*
;
import org
.springframework
.stereotype
.Controller
;
import org
.springframework
.web
.bind
.annotation
.*
;
import javax
.annotation
.Resource
;
import javax
.servlet
.http
.HttpServletRequest
;
import java
.util
.List
;
@RestController
@Api(tags
= "学生功能")
public class StudentController {
@Resource
private StudentService studentService
;
@ApiOperation(value
= "验证登录的方法")
@ApiImplicitParams({
@ApiImplicitParam(name
= "sid",required
= true,paramType
= "path"),
@ApiImplicitParam(name
= "pwd",required
= true,paramType
= "path")
})
@ApiResponses({
@ApiResponse(code
= 510,message
= "数据库操作错误"),
@ApiResponse(code
= 503,message
= "参数不准确")
})
@PostMapping("/doLogin/{sid}/{pwd}")
public ReturnResult
doLogin(@PathVariable("sid") int sid
,
@PathVariable("pwd") String pwd
){
try {
Student student
= studentService
.getStuBySid(sid
);
if (student
!=null
){
if (student
.getPassword().equals(pwd
)){
if (student
.getLevel()>=4){
return ReturnUtils
.returnSuccessWithData(student
,"说明是老师");
}else {
return ReturnUtils
.returnSuccessWithData(student
,"说明是学生");
}
}else {
return ReturnUtils
.returnFailed("503");
}
}else {
return ReturnUtils
.returnFailed("503");
}
} catch (Exception e
) {
e
.printStackTrace();
return ReturnUtils
.returnFailed("510");
}
}
}
ReturnUtils是一个封装的工具类代码如下: 实体类
package com
.azapp
.demo
.swaggertest
.entity
;
import io
.swagger
.annotations
.ApiModel
;
import io
.swagger
.annotations
.ApiModelProperty
;
@ApiModel
public class ReturnResult<T> {
@ApiModelProperty("状态码,200表示成功")
private String code
;
@ApiModelProperty("描述信息,偶尔会有")
private String msg
;
@ApiModelProperty("回传的数据")
private T data
;
public String
getCode() {
return code
;
}
public void setCode(String code
) {
this.code
= code
;
}
public String
getMsg() {
return msg
;
}
public void setMsg(String msg
) {
this.msg
= msg
;
}
public T
getData() {
return data
;
}
public void setData(T data
) {
this.data
= data
;
}
}
工具类
package com
.azapp
.demo
.swaggertest
.utils
;
import com
.azapp
.demo
.swaggertest
.entity
.ReturnResult
;
public class ReturnUtils {
public static ReturnResult
returnSuccess(){
ReturnResult result
=new ReturnResult();
result
.setCode("200");
return result
;
}
public static ReturnResult
returnSuccess(String msg
){
ReturnResult result
=new ReturnResult();
result
.setCode("200");
result
.setMsg(msg
);
return result
;
}
public static ReturnResult
returnSuccessWithData(Object data
){
ReturnResult result
=new ReturnResult();
result
.setCode("200");
result
.setData(data
);
return result
;
}
public static ReturnResult
returnSuccessWithData(Object data
,String msg
){
ReturnResult result
=new ReturnResult();
result
.setCode("200");
result
.setData(data
);
result
.setMsg(msg
);
return result
;
}
public static ReturnResult
returnFailed(String code
){
ReturnResult result
=new ReturnResult();
result
.setCode(code
);
return result
;
}
public static ReturnResult
returnFailed(String code
,String msg
){
ReturnResult result
=new ReturnResult();
result
.setCode(code
);
result
.setMsg(msg
);
return result
;
}
public static ReturnResult
returnFailedWithData(String code
,String msg
,Object data
){
ReturnResult result
=new ReturnResult();
result
.setCode(code
);
result
.setMsg(msg
);
result
.setData(data
);
return result
;
}
}