- 首先需要引入几个maven依赖,pom.xml如下
<!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.5.0</version> </dependency> <!-- swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>28.0-jre</version> </dependency>
- 配置config
@EnableSwagger2 @Configuration public class SwaggerConfig { @Bean public Docket swaggerSpringMvcPlugin() { return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build(); } }
- 配置controller
public class InterfaceDocumentation { /** * 创建个人账号,返回的accountId会在创建企业账号时需要 * * @param name 姓名 * @param idNo 证件号 * @param idType 个人账号证件类型 * @param mobile 手机号 * @param email 邮箱 * @return */ @ApiOperation(value = "创建个人账号", notes = "创建企业账号之前必须要先调用此接口创建个人账号") @ApiImplicitParams({ @ApiImplicitParam(name = "name", value = "用户姓名", required = true, dataType = "String", paramType = "query"), @ApiImplicitParam(name = "idNo", value = "证件号", required = true, dataType = "String", paramType = "query"), @ApiImplicitParam(name = "idType", value = "个人账号证件类型", required = true, dataType = "int", paramType = "query"), @ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType = "String", paramType = "query"), @ApiImplicitParam(name = "email", value = "邮箱", required = false, dataType = "String", paramType = "query"), }) @RequestMapping(value = "/account/create/person", method = RequestMethod.POST) public String createPersonAccount(@RequestParam("name") String name, @RequestParam("idNo") String idNo, @RequestParam("idType") Integer idType, @RequestParam("mobile") String mobile, @RequestParam(value = "email", required = false) String email) { return ""; } }
- 效果图展示
注意事项:当springboot中使用了thymeleaf模板时访问swagger-ui.html出现404或重定向问题,加上以下代码即可