springboot集成swagger2
官网
https://swagger.io/
1:pom
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
2:配置
测试环境打开,生成环境关闭,否则影响效率
import org.springframework.beans.factory.annotation.Value;
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.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2 {
@Value("${swagger.show}")
private boolean swaggerShow;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(swaggerShow)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.luischen.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Luis Site Swagger Restful API")
.description("更多Spring Boot相关文章请关注:https://swagger.io/")
.termsOfServiceUrl("https://swagger.io/")
.contact("Luis chen")
.version("1.0")
.build();
}
}
3:访问
http://localhost:8080/swagger-ui.html
页面就出来了, 看似没有配置,实际使用的为默认配置
4:接口配置:注解都是@APIxx,会在日志里打印
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiImplicitParam:描述一个请求参数,可以配置参数的中文含义,还可以给参数设置默认值
@ApiImplicitParams:描述由多个 @ApiImplicitParam 注解的参数组成的请求参数列表
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
/**
* 首页和关于我的页面控制器
*
*/
@Api("网站首页和关于页面")
@Controller
public class HomeController extends BaseController{
@Autowired
private ContentService contentService;
@ApiIgnore
@GetMapping(value = {"/about", "/about/index"})
public String getAbout(HttpServletRequest request){
this.blogBaseData(request, null);//获取友链
request.setAttribute("active","about");
return "site/about";
}
@ApiOperation("blog首页")
@GetMapping(value = {"/blog/","/blog/index"})
public String blogIndex(
HttpServletRequest request,
@ApiParam(name = "limit", value = "页数", required = false)
@RequestParam(name = "limit", required = false, defaultValue = "11")
int limit
){
return this.blogIndex(request, 1, limit);
}
@ApiOperation("blog首页-分页")
@GetMapping(value = "/blog/page/{p}")
public String blogIndex(
HttpServletRequest request,
@PathVariable("p")
int p,
@RequestParam(value = "limit", required = false, defaultValue = "11")
int limit
){
p = p < 0 || p > WebConst.MAX_PAGE ? 1 : p;
ContentCond contentCond = new ContentCond();
contentCond.setType(Types.ARTICLE.getType());
PageInfo<ContentDomain> articles = contentService.getArticlesByCond(contentCond, p, limit);
request.setAttribute("articles", articles);//文章列表
request.setAttribute("type", "articles");
request.setAttribute("active", "blog");
// this.blogBaseData(request, contentCond);//获取公共分类标签等数据
return "site/blog";
}
}
5:可以实时更新接口,方便前后台联调,接口测试