因为开发项目的接口需要给app,小程序测试,所以用swagger.
1.pom.xml:
<dependency><!--添加Swagger依赖 --> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency><!--添加Swagger-UI依赖 --> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
2.写一个java的配置文件:
package com.atguigu.springboot.config; 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 { /** * 添加摘要信息(Docket) */ @Bean public Docket controllerApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(new ApiInfoBuilder() .title("书法管理系统_接口文档") .description("书法用于管理集团旗下公司的人员信息,具体包括") .contact(new Contact("小猪不笨", null, null)) .version("版本号:1.0") .build()) .select() .apis(RequestHandlerSelectors.basePackage("com.atguigu.springboot.controller")) .paths(PathSelectors.any()) .build(); } }
3.在Controller打上swagger常用的注解:
@Api(value="部门管理基础api文档") @RestController @RequestMapping(value="/dept") public class DeptController { @Autowired DeptMapper deptMapper; /* @ApiOperation(value="查询单个部门") @GetMapping("/getById") public DeptEntity getById(){ Integer id = 1; return deptMapper.getById(id); }*/ /* @ResponseBody @RequestMapping(value ="/getUserName", method= RequestMethod.GET) @ApiOperation(value="根据用户编号获取用户姓名", notes="test: 仅1和2有正确返回") @ApiImplicitParam(paramType="query", name = "userNumber", value = "用户编号", required = true, dataType = "Integer") public String getUserName(@RequestParam Integer userNumber){ if(userNumber == 1){ return "张三丰"; } else if(userNumber == 2){ return "慕容复"; } else{ return "未知"; } } @ResponseBody @RequestMapping("/updatePassword") @ApiOperation(value="修改用户密码", notes="根据用户id修改密码") @ApiImplicitParams({ @ApiImplicitParam(paramType="query", name = "userId", value = "用户ID", required = true, dataType = "Integer"), @ApiImplicitParam(paramType="query", name = "password", value = "旧密码", required = true, dataType = "String"), @ApiImplicitParam(paramType="query", name = "newPassword", value = "新密码", required = true, dataType = "String") }) public String updatePassword(@RequestParam(value="userId") Integer userId, @RequestParam(value="password") String password, @RequestParam(value="newPassword") String newPassword){ if(userId <= 0 || userId > 2){ return "未知的用户"; } return "密码修改成功!"; }*/ }
简单方便.