swagger提供了html页面方便查看restful的api接口,以及测试功能,这样方便后期版本更新时的文档与代码的同步。
以下是整合步骤
一:在spring项目中添加依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
二:创建类名为swagger的类,代码如下:
package testER.cn.sun;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* 初始swagger的配置
* @version:2018年4月17日 上午10:01:03
* @author WuJianGang
*/
@Configuration
@EnableSwagger2
public class swagger {
@Bean
public Docket createRestApi() {// 创建API基本信息
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("testER.cn.sun"))// 扫描该包下的所有需要在Swagger中展示的API,@ApiIgnore注解标注的除外
.paths(PathSelectors.any()) // 对所有路径进行监控
.build();
}
public ApiInfo apiInfo() {// 创建API的基本信息,这些信息会在Swagger UI中进行显示
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")// API 标题
.description("rdcloud-jpa提供的RESTful APIs")// API描述
.contact("wjg")// 联系人
.version("1.0")// 版本号
.build();
}
}
三:创建一个为controller的类,完善API文档信息
package testER.cn.sun;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
/**
* Swagger常用注解:
* @Api:修饰整个类,描述Controller的作用
* @ApiOperation:描述一个类的一个方法,或者说一个接口
* @ApiParam:单个参数描述
* @ApiModel:用对象来接收参数
* @ApiProperty:用对象参数,描述对象的一个字段
* @ApiIgnore:使用该注解时忽略这个API
* @ApiResponse:HTTP响应其中一个描述
* @ApiResponses:HTTP响应整个描述
*/
@RestController
@RequestMapping("/user")
@Api(value = "shop")
public class controller {
@ApiOperation(value = "获取helloworld", notes = "简单的Spring boot请求")
@RequestMapping("/a")
String home() {
return "HELLO SWAGGER";
}
@ApiOperation(value = "获得参数", notes = "根据参数中的classNo和studentName是的字符串相加")
@ApiImplicitParams({ @ApiImplicitParam(name = "classNo", value = "班级编号", required = true, dataType = "String") })
@RequestMapping(value = "/class/{classNo}/to/{studentName}", method = RequestMethod.GET)
String world(@PathVariable("classNo") String classNo, @PathVariable("studentName") String studentName) {
return classNo + " " + studentName;
}
@ApiOperation(value="获取awagger",notes="简单的swagger测试")
@RequestMapping("/b")
String awa() {
return "hello test";
}
}
四:进入UI界面查看效果
http://localhost:8080/swagger-ui.html