1. 在idea的pom.xml中引入swagger依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.0</version>
</dependency>
2. 配置swaggerconfiguration
package com.example.swagger;
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;
/**
* 访问路径:http://localhost:7777/swagger-ui.html
* 端口号是application中设置的端口号*/
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@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("我的swagger接口文档")//文档名
.description("接口文档")//文档描述
.contact(new Contact("ybb","","123@qq.com"))//作者名,url和邮箱
.version("1.0")//文档版本
.build();
}
}
3. 创建Application类
package scanapplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
//托管
@SpringBootApplication
//扫描包下的类(双引号不能忘)
@ComponentScan("com.example")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4. 编写接口类,get或者post都可以。
get接口
package com.example.demo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
@RestController
@Api(value = "/")
public class MyGetTest {
// public HttpServletResponse response;
//路径和方法
@RequestMapping(value = "/getCookies",method = RequestMethod.GET)
@ApiOperation(value = "获取cookies",httpMethod = "GET")//接口后面的解释文字和请求方法
public String getCookies(HttpServletResponse response){
// HttpServerletRequest
Cookie cookie = new Cookie("login","true");
// HttpServerletReponse
response.addCookie(cookie);
return "成功";
}
}
post接口
package com.example.demo;
import com.example.bean.User;
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;
import javax.servlet.http.HttpServletResponse;
/**
* 登录接口*/
@RestController
@RequestMapping("v1")
@Api(value = "/")
public class MyPostTest {
private static Cookie cookie;
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ApiOperation(value = "登录接口,获取cookie信息", httpMethod = "POST")
public String login(HttpServletResponse response,
@RequestParam(value = "username", required = true) String username,
@RequestParam(value = "password", required = true) String password) {
if (username.equals("zhangsan") && password.equals("111111")) {
cookie = new Cookie("login", "true");
response.addCookie(cookie);
return "登录成功";
}
return "登录失败";
}
@RequestMapping(value = "/getUserList",method = RequestMethod.POST)
@ApiOperation(value = "/获取用户列表",httpMethod = "POST")
public String getUserList(HttpServletRequest request,
@RequestBody User u){
//获取cookies
Cookie[] cookies = request.getCookies();
//验证cookies是否合法
for (Cookie c:cookies){
//equals和==
if (c.getName().equals("login")
&&c.getValue().equals("true")
&&u.getUsername().equals("zhangsan")
&&u.getPassword().equals("123456")
){
User user = new User();
user.setName("lisi");
user.setAge("18");
user.setSex("man");
return user.toString();
}
}
return "参数不正确";
}
}
5. 包结构,尽量把接口类和swagger的配置类放在同一个大包下。
6. 启动application类,访问swagger的网址如下:http://localhost:7777/swagger-ui.html ,固定写法,端口号在application的配置文件中配置即可。
7. 运行成功的swagger页面
注意:这里的swagger页面会显示basic-error-controller : Basic Error Controller,解决方法是在swagger configuration中的api方法加入两行代码。
.apis(RequestHandlerSelectors.any())// 对所有api进行监控
// 不显示错误的接口地址
.paths(Predicates.not(PathSelectors.regex("/error.*")))//错误路径不监控