赞
踩
- <!--加入springboot-->
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.3.RELEASE</version>
- <relativePath />
- </parent>
- <!--JDK版本-->
- <properties>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <!--Springboot web和测试依赖 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <!-- 有更改时重新启动springboot方便调试-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <scope>runtime</scope>
- </dependency>
- <!-- swagger2依赖 -->
- <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>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-staticdocs</artifactId>
- <version>2.5.0</version>
- </dependency>
- </dependencies>

2. 建立springboot启动类
- package com.tpdog.app;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- import springfox.documentation.swagger2.annotations.EnableSwagger2;
-
- @SpringBootApplication
- @EnableSwagger2
- public class TpdogApp {
-
- public static void main(String[] args) {
- SpringApplication.run(TpdogApp.class, args);
- System.out.println("springboot启动成功");
- }
- }

建立swagger2配置类
- package com.tpdog.app;
-
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
-
- import springfox.documentation.builders.ApiInfoBuilder;
- import springfox.documentation.builders.PathSelectors;
- import springfox.documentation.builders.RequestHandlerSelectors;
- import springfox.documentation.service.ApiInfo;
- import springfox.documentation.service.Contact;
- import springfox.documentation.spi.DocumentationType;
- import springfox.documentation.spring.web.plugins.Docket;
- import springfox.documentation.swagger2.annotations.EnableSwagger2;
-
- @ComponentScan("com.tpdog.controller")//需要扫描的api位置
- @Configuration//说明是一个配置类
- @EnableSwagger2//支持swagger2
- public class SwaggerConfig {
-
- /**
- * 创建BeanApi
- * apiInfo() 增加API相关信息
- * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
- * 本例采用指定扫描的包路径来定义指定要建立API的目录。
- *
- * @return
- */
- @Bean
- public Docket createRestApi() {
- return new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo())
- .select()
- .apis(RequestHandlerSelectors.basePackage("com.tpdog"))//扫描Api包
- .paths(PathSelectors.any())
- .build();
- }
-
- /**
- * 创建该API的基本信息(这些基本信息会展现在文档页面中)
- * 访问地址:http://项目实际地址/swagger-ui.html
- * @return
- */
- private ApiInfo apiInfo() {
- return new ApiInfoBuilder()
- .title("Springboot-Swagger示例")//api的标日
- .description("一般常用api信息")//api描述
- .termsOfServiceUrl("https://www.baidu.com/")//服务地址
- .contact(new Contact("cqc", "https://www.baidu.com/", "leomple@163.com"))//联系信息
- .version("1.0")
- .build();
- }
- }

3. 编写API类
- package com.tpdog.controller;
-
- import javax.servlet.http.HttpServletRequest;
-
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- 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 io.swagger.annotations.Api;
- import io.swagger.annotations.ApiImplicitParam;
- import io.swagger.annotations.ApiOperation;
-
- @Controller
- @RequestMapping("/dateapi")
- @Api(value = "日期管理API")
- public class DateController {
-
- @RequestMapping(value = "/date", method = RequestMethod.GET)
- @ResponseBody
- @ApiImplicitParam(paramType = "query",name= "cityid" ,value = "城市id",dataType = "string")
- @ApiOperation(value = "通过城市id获取天气信息", notes="返回城市天气信息")
- public ResponseEntity<Object> getDateInfo(HttpServletRequest request) {
- return new ResponseEntity<>(HolidayApi.getHoliday("s"), HttpStatus.OK);
- }
- }

4. 启动Springboot启动类,在浏览器中输入:http://localhost:8099/swagger-ui.html
记住两处的端口要对应,默认的是8080,如需修改端口则在src/main/resources下建立application.properties文件并加入server.port=8099 然后保存,8099随意加的可以加入自己喜欢的四位端口号,值尽量大一些,推荐使用8080以后的重新启动Springboot启动类在浏览器中输入:http://localhost:8099/swagger-ui.html访问
一般常见问题,下面问题要修改端口号,参照4当中的操作,修改端口号。访问api地址不显示controller,是由于在SwaggerConfig类中没有加入@ComponentScan("你的controller包路径"),扫描api包的路径即可显示
种一棵树最好的时间是十年前,其次是现在。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。