SwaggerUI是自动生成接口文档的工具
1、Maven配置
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent>
<!--Springboot启动--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.0.5.RELEASE</version> </dependency>
2、SwaggerConfig类配置
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.pathMapping("/")
.select()
.paths(PathSelectors.regex("/.*"))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("我的接口文档")
.contact(new Contact("dazhou","","42197393@qq.com"))
.description("这是我的swaggerui生成的接口文档")
.version("1.0.0.0")
.build();
}
3、接口文档类使用
类上使用“@Api”标签
@Api(value = "/",description = "这是我所有的get请求")
方法上使用“@ApiOperation”标签
@ApiOperation(value = "带有Cookies信息的Get请求",httpMethod = "GET")
package com.config.hello;
import com.sun.deploy.net.HttpRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
@RestController
@Api(value = "/",description = "这是我所有的get请求")
public class getWithCookies {
@RequestMapping(value = "/getWithCookie",method = RequestMethod.GET)
@ApiOperation(value = "带有Cookies信息的Get请求",httpMethod = "GET")
public String getWithCookiesMethod(HttpServletRequest request){
Cookie [] cookies = request.getCookies();
for(Cookie cookie: cookies){
if(cookie.getName().equals("login")&& cookie.getValue().equals("true")){
return "恭喜登录成功";
}
}
return "您必须,携带Cookies信息才能登录";
}
/**
* 带参数的get请求方法1
* */
@RequestMapping(value = "/get/with/param",method = RequestMethod.GET)
@ApiOperation(value = "带有参数信息的Get请求一",httpMethod = "GET")
public String getWithParam(@RequestParam String start,
@RequestParam String end){
return "恭喜登录成功";
}
/**
* 带参数的get请求方法二
* */
@ApiOperation(value = "带有参数信息的Get请求二",httpMethod = "GET")
@RequestMapping(value = "/get/with/param/{start}/{end}",method = RequestMethod.GET)
public String getWithParamMethod2(@PathVariable Integer start,
@PathVariable Integer end){
return "恭喜登录成功了";
}
}
4、生成的接口文档访问页面
http://localhost:8080/swagger-ui.html
注意端口号配置信息