1. pom
<!-- swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-ui</artifactId> <version>2.0.2</version> </dependency>
2. 代码配置
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(createApiInfo()) .select().apis(RequestHandlerSelectors .basePackage("com.demo.controller")) // 对应项目的controller目录 .paths(PathSelectors.any()) .build(); } private ApiInfo createApiInfo() { return new ApiInfoBuilder() .title("主题") .description("描述信息") .termsOfServiceUrl("http://localhost") .version("1.0.0") .contact(new Contact("张叔叔", "http://localhost", "email@com")) .build(); } }
3. controller中添加 swagger注解
@Slf4j @Api("用户控制器") @RestController @RequestMapping(value = "/api/v1/demo_one/user", produces = "application/json") public class UserController implements DemoOneService { @RequestMapping(value = "/say", method = RequestMethod.GET) public String hello(@RequestParam("name") String name) { return "Hello " + name; } }
4. 登录swagger的ui界面查看在线API信息
http://localhost:port/swagger-ui.html
http://localhost:port/doc.html#