1、引用依赖包
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.5.0</version> </dependency <dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.5.0</version> </dependency>
2、新建 SwaggerConfig 类
package cn.com.xxx.config;import io.swagger.annotations.ApiOperation; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration @EnableSwagger2 public class SwaggerConfig {public Docket swaggerSpringMvcPlugin(){return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build();} }
3、配置接口类
package cn.com.xxx.controller;import cn.com.xxx.dao.AccountDao; import cn.com.xxx.po.T_Account; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;@Api(value = "") @RestController @RequestMapping(value = "/api/test") public class ApiTestController {@Autowiredprivate AccountDao accountDao;@ApiOperation(nickname = "获取人员信息",value = "账号")@RequestMapping(value = "/getAccountByUserName/{userName}",method = RequestMethod.GET)public T_Account getAccountByUserName(@PathVariable("userName") String userName){return accountDao.findUserInfoByUserName(userName);} }
4、配置spring boot启动类
package cn.com.xxx;import org.mybatis.spring.annotation.MapperScan; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList; import java.util.List;/*** Hello world!**/ @MapperScan(basePackages = {"cn.com.xxx.dao"}) @SpringBootApplication(scanBasePackages = {"cn.com.xxx"}) public class App {private static final Logger logger = LoggerFactory.getLogger(App.class);public static void main( String[] args ){logger.info("开始启动");SpringApplication.run(App.class,args);logger.info("启动结束");}}
5、启动spring boot并访问:http://localhost:端口/swagger-ui.html
6、输入测试数据并获取结果
至此集成spring+swagger集成结束。