spring-boot+swagger实现WebApi文档
生活随笔
收集整理的這篇文章主要介紹了
spring-boot+swagger实现WebApi文档
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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、輸入測試數(shù)據(jù)并獲取結果
?
?至此集成spring+swagger集成結束。
?
轉載于:https://www.cnblogs.com/umeall/p/10594206.html
總結
以上是生活随笔為你收集整理的spring-boot+swagger实现WebApi文档的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Msfvenom木马使用及TheFatR
- 下一篇: 使用Docker 安装jdk8