1.添加swagger依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 接口API生成html文档 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!--解决控制层接受参数为Integer类似的参数,访问swagger页面报错-->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.21</version>
</dependency>
2.在项目启动类的同级处添加swagger配置类
package com.iflytek.edu.hnezzhxy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
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;
/**
* @description swagger2文档
* @version 1.0
* @create 2020/06/19 08:31
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//"com.iflytek.edu.hnezzhxy.controller", web层所在的目录
.apis(RequestHandlerSelectors.basePackage("com.iflytek.edu.hnezzhxy.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot利用swagger构建api文档---淮南第二中学招生系统")
.description("简单优雅的restfun风格")
.termsOfServiceUrl("")
.version("1.0")
.build();
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
// 解决静态资源无法访问
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/");
// 解决swagger无法访问
registry.addResourceHandler("/swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
// 解决swagger的js文件无法访问
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
3.控制层添加一个方法
package com.iflytek.edu.hnezzhxy.controller;
import com.iflytek.edu.hnezzhxy.common.enums.ResponseCodeEnum;
import com.iflytek.edu.hnezzhxy.dao.ZsbmUserDao;
import com.iflytek.edu.hnezzhxy.model.ZsbmUser;
import com.iflytek.edu.hnezzhxy.util.ResponseResultUtil;
import com.iflytek.edu.hnezzhxy.vo.ResultVO;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpSession;
/**
* @description 登陆处理类
* @create 2020/06/18 15:44
* @version 1.0
*/
@RestController
@Validated
@Api(tags = "登陆相关接口")
public class LoginController {
/**
* @description 退出登录
* @param session
* @return ResultVO
*/
@RequestMapping("/getLoginOut")
@ApiOperation(value="退出登陆",httpMethod ="GET", notes="退出登陆")
public ResultVO getLoginOut(HttpSession session){
Object object = session.getAttribute(Constants.SESSION_USER_Attribute);
if(object!=null){
session.removeAttribute(Constants.SESSION_USER_Attribute);
}
return new ResponseResultUtil().success(ResponseCodeEnum.LOGINOUT_SUCCESS.getCode(),
ResponseCodeEnum.LOGINOUT_SUCCESS.getMessage(),null,true);
}
}
4.访问swagger文档
http://localhost:8081/hnezzsbm/swagger-ui.html#
5.效果图
6友情提示
若你的swgger文档接口说明不是中文。请在对应的controller层添加tags=“你的中文描述”,若是配置了此中文,swagger接口详情信息点不进去,那么请将你的swgger依赖版本更新至2.8以上