1.引入jar包
<properties>
<swagger.version>2.6.1</swagger.version>
</properties>
<dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies>
2.新建配置文件
在main/java目录下,新建com.course.config目录以存放配置文件,然后新建SwaggerConfig。
@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("Lyn","","326030434@qq.com"))
.description("这是SwaggerUi生成的接口文档")
.build();
}
}
重启springboot,访问http://localhost:8080/swagger-ui.html,没有拿到所写的接口信息,原因是接口中没有配置swagger配置
3.给接口加上注解
首先要在整个类前面加
@Api(value = "/",description = "这是所有get方法")
然后在每个方法上加
@ApiOperation(value = "通过这个方法可以获取到cookies",httpMethod = "GET")
@RestController
@Api(value = "/", description = "这是我全部的get方法")
public class MyGetMethod {
@RequestMapping(value = "/getCookies", method = RequestMethod.GET)
@ApiOperation(value = "通过这个方法可以获取cookies", httpMethod = "GET")
public String getCookies(HttpServletResponse response) {
//HttpServerletRequest 装请求信息的类
//HttpServerletResponse 装响应信息的类
Cookie cookie = new Cookie("login", "true");
response.addCookie(cookie);
return "恭喜你获得cookies信息成功";
}
/**
* 这是一个需要携带cookies信息才能访问的get请求
*/
@RequestMapping(value = "get/with/cookies", method = RequestMethod.GET)
@ApiOperation(value = "这是一个需要携带cookies信息才能访问的get请求", httpMethod = "GET")
public String getWithCookies(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
if (cookies == null) {
return "no cookies with get method";
}
for (Cookie cookie : cookies) {
if (cookie.getName().equals("login") && cookie.getValue().equals("true")) {
return "这是一个需要携带cookies信息才能访问的get请求";
}
}
return "cookies info wrong";
}
/**
* 开发一个需要携带参数才能访问的get请求
* 第一种实现方式:url:key=value&key=value
* 我们来模拟获取商品列表
*/
@RequestMapping(value = "/get/with/param", method = RequestMethod.GET)
@ApiOperation(value = "需要携带参数才能访问的get请求方法一", httpMethod = "GET")
public Map<String, Integer> getList(@RequestParam Integer start,
@RequestParam Integer end) {
Map<String, Integer> myLsit = new HashMap<String, Integer>();
myLsit.put("鞋子", 400);
myLsit.put("方便面", 5);
myLsit.put("口红", 300);
return myLsit;
}
/**
* 开发一个需要携带参数才能访问的get请求
* 第二种实现方式:url:url:port/get/with/param/10/20
* 我们来模拟获取商品列表
*/
@RequestMapping(value = "/get/with/param/{start}/{end}", method = RequestMethod.GET)
@ApiOperation(value = "需要携带参数才能访问的get请求方法二", httpMethod = "GET")
public Map<String, Integer> getMyList(@PathVariable Integer start,
@PathVariable Integer end) {
Map<String, Integer> myLsit = new HashMap<String, Integer>();
myLsit.put("鞋子", 400);
myLsit.put("方便面", 5);
myLsit.put("口红", 300);
return myLsit;
}
/**
* 开发一个需要携带参数和cookies才能访问的get请求
* 我们来模拟用户使用用户名和密码登录
*/
@RequestMapping(value = "/get/with/paramAndCookie", method = RequestMethod.GET)
@ApiOperation(value = "需要携带参数和cookies才能访问的get请求", httpMethod = "GET")
public String login(HttpServletRequest request,
@RequestParam String name,
@RequestParam String password) {
Cookie[] cookies = request.getCookies();
if (cookies == null) {
return "请配置cookie信息";
}
for (Cookie cookie : cookies) {
if (cookie.getName().equals("login") & cookie.getValue().equals("true")) {
if (name.equals("zhangsan") && password.equals("123456")) {
return "zhangsan 登录成功";
} else if (name.equals("zhangsan") && !password.equals("123456")) {
return "密码错误请重试";
} else {
return "请注册再登录";
}
}
}
return "请注册再登录";
}
}
4.修改Application文件
之前是@ComponentScan(“com.course.server”)并没有包含到com.course.config文件,所以,这里修改为@ComponentScan(“com.course”)
@SpringBootApplication
@ComponentScan("com.course")
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}
5.查看接口文档
运行 Application文件,然后浏览器访问http://localhost:9000/swagger-ui.html
6.测试接口
可以在该网站中对接口做简单的测试工作:找到某个具体的测试接口输入入参后,点击“Cry it out”