在springboot項目中 集成swagger :
1.添加swagger依賴,以下java
<!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version> </dependency> <!-- swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version> </dependency>
2.配置 SwaggerConfig 以下:spring
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket swaggerSpringMvcPlugin() { return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build(); } }
3.寫controller:api
@Api(description = "用戶接口") @RestController public class DemoController { private static final Logger logger = LoggerFactory.getLogger(DemoController.class); @ApiOperation(value = "新增用戶" , notes="新增註冊") @RequestMapping(value="/createUser",method=RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE) public ResObject createUser(@RequestBody User user){ logger.info("用戶信息:"+user.toString()); return new ResObject(HttpStatus.OK.value(), "新增成功."); } }
另外,還有在線的api接口文檔管理平臺 http://www.sosoapi.com/auth/home/home.htmspringboot