-
导入依赖
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
-
配置启动类
@SpringBootApplication @EnableDiscoveryClient @MapperScan("com.bajie.user.mapper") @EnableSwagger2 public class UserServiceApp { public static void main(String[] args) { SpringApplication.run(UserServiceApp.class,args); } }
-
配置配置文件
@Configuration public class Swagger2Configuration { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.bajie")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("八戒api文档") .description("八戒api文档") // .termsOfServiceUrl("/") .version("1.0") .build(); } }
-
使用
@RestController @RequestMapping @Api(value="bajie用户管理接口",description = "bajie用户管理接口,提供页面的增、删、改、查") public class UserController { @Autowired private UserService userService; @GetMapping("check/{data}/{type}") @ApiOperation("分页查询页面列表") @ApiImplicitParams({ @ApiImplicitParam(name="data",value = "数据",required=true,paramType="path",dataType="int"), @ApiImplicitParam(name="type",value = "类型",required=true,paramType="path",dataType="int") }) public ResponseEntity<Boolean> checkUserData(@PathVariable("data") String data, @PathVariable(value = "type",required = true) Integer type) { Boolean boo = this.userService.checkData(data, type); if (boo == null) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); } return ResponseEntity.ok(boo); } }
-
访问http://localhost:25000/swagger-ui.html