Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。 文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。
作用:
1. 接口的文档在线自动生成。
2. 功能测试。
1.添加pom.xml
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency>
2.在启动类同级增加类
package com; 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 Swagger2 { //swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等 @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //为当前包路径 .apis(RequestHandlerSelectors.basePackage("com.taiji.demo.controller")) .paths(PathSelectors.any()) .build(); } //构建 api文档的详细信息函数,注意这里的注解引用的是哪个 private ApiInfo apiInfo() { return new ApiInfoBuilder() //页面标题 .title("系统API接口") //创建人 .contact(new Contact("zhangyg", "www.redxun.com", "zhangyg@redxun.cn")) //版本号 .version("1.0") //描述 .description("系统API接口") .build(); } }
3.编辑java 文件
package com.taiji.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.alibaba.fastjson.JSONObject; import com.taiji.core.util.IdUtil; import com.taiji.demo.model.SaleOrder; import com.taiji.demo.service.SaleOrderService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; @RestController @Api(value="销售订单API接口") public class SaleOrderController { @Autowired private SaleOrderService saleOrderService; @ApiOperation(value="添加订单",notes="添加订单描述") @RequestMapping(value="/add",method={RequestMethod.POST,RequestMethod.GET}) public void add(@ApiParam(value = "用户数据", required = true, type="JSONObject", example="{"name":"A","userId":"B","total":"100"}") @RequestBody JSONObject json){ SaleOrder order=new SaleOrder(); order.setId(IdUtil.getId()); order.setName(json.getString("name")); order.setCreator(json.getString("userId")); order.setTotal(json.getDouble("total")); saleOrderService.create(order); } @ApiOperation(value="根据ID查看明细",notes="根据ID查看明细") @ApiImplicitParams(value={ @ApiImplicitParam(name="id",value="订单ID",required=true,dataType="String") }) @RequestMapping(value="/get/{id}",method=RequestMethod.GET) public SaleOrder get(@PathVariable(value="id") String id){ SaleOrder order= saleOrderService.get(id); return order; } @RequestMapping(value="/page/{page}",method=RequestMethod.GET) @ApiResponses({ @ApiResponse(response=JSONObject.class, code = 200, message = "订单列表") }) public JSONObject page(@PathVariable(value="page") int page){ JSONObject order= saleOrderService.getAll(page,10); return order; } }
4.访问平台列表
http://localhost:8000/demo/swagger-ui.html#/sale45order45controller
5.在线调试接口
输入好参数后,点击try it out 按钮,可以对代码进行调试。