以前调用公司同事接口,就给了我一个wagger地址,我一看,这么多接口,还可以在线调用,很方便,如今自己看视频看到这个配置,手痒也想搞一个,通过BiliBili网站学习,和百度搜索,终于搞整出来了。
最大的问题是兼容问题,只要配置好一次,以后就可以用一样的配置了。
感谢大佬(解决方案之‘Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPoi_技术宅星云的博客-CSDN博客)的详细说明,本想走【修复方案二】,节约时间,先降低版本,所幸能解决。
1. 在pom.xml添加Swagger2依赖
<!-- 使用Swagger生成Web API文档 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2.新建一个config包,下面创建一个SwaggerConfig类,这个类在网上拷贝修改了下,感觉比视频里看的,功能要丰富点。不大明白的地方可以看注释
package com.ceaning.efmis.config;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration//告诉Sping容器 这个类是一个配置类
@EnableSwagger2//启用Swagger2功能
public class SwaggerConfig {
// swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("EFMIS-V1.0")
.select()
//设定扫描接口的包basePackage(""),withClassAnnotation(RestController.class))
.apis(RequestHandlerSelectors.basePackage("com.ceaning.efmis.controller"))
//过滤某些访问路径下的接口eg:扫描一个controller下的 /hello/xxx路径下的接口
.paths(PathSelectors.any())
.build();
}
// 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 页面标题
.title("EFMIS API")
// 创建人信息
.contact(new Contact("XXX", "", "XXXXXXX@qq.com"))
// 版本号
.version("1.0")
// 描述
.description("API 接口描述")
.build();
}
}
3. 在application.properties下添加,这一步有点坑,我的SpingBoot原本是2.7.5的,后来出现兼容问题,我不得不改到2.5.6去(菜鸡一枚,最怕这种耗时的问题,其实就可以不要一步,但我还是加上了)
#防止高版本springboot 2.6.x以上与Swagger的不兼容问题
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
4. controller类就可以进行注解说明, 比如我的UserController
package com.ceaning.efmis.controller;
import com.ceaning.efmis.mapper.UserMapper;
import com.ceaning.efmis.pojo.User;
import com.ceaning.efmis.utils.Log4jUtils;
import com.ceaning.efmis.utils.MybatisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.session.SqlSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api")
@Api(value = "账号管理",tags = "账号管理")
public class UserController {
@GetMapping("/user/getUserList")
@ApiOperation(value = "获取所有账号信息列表",notes = "获取所有账号信息列表")
public List<User> getUserList(){
List<User> userList= null;
SqlSession sqlSession= null;
try{
sqlSession= MybatisUtils.getSqlSession();
//Mapper代理开发
UserMapper mapper= sqlSession.getMapper(UserMapper.class);
userList= mapper.getUserList();
Log4jUtils.logger.info("asd");
// for (User user: userList){
// System.out.println(user);
// }
} catch (Exception e){
e.printStackTrace();
} finally {
if (sqlSession!= null) {
sqlSession.close();
}
}
return userList;
}
@PostMapping("/user/findUser")
@ApiOperation(value = "根据账号和密码获取特定账号信息",notes = "获取特定账号信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "username", value = "账号", dataType = "string", paramType = "query", example = "10086", required = true),
@ApiImplicitParam(name = "password", value = "密码", dataType = "string", paramType = "query", example = "123qwe,.", required = true)
})
public User findUser(String username, String password){
User user= null;
SqlSession sqlSession= null;
try{
sqlSession= MybatisUtils.getSqlSession();
UserMapper mapper= sqlSession.getMapper(UserMapper.class);
user= mapper.findUser(username,password);
} catch (Exception e){
e.printStackTrace();
} finally {
if (sqlSession!= null){
sqlSession.close();
}
}
return user;
}
@PostMapping("/user/updateUser")
@ApiOperation(value = "更新特定账号信息",notes = "更新特定账号信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "username", value = "账号", dataType = "string", paramType = "query", example = "10086", required = true),
@ApiImplicitParam(name = "password", value = "密码", dataType = "string", paramType = "query", example = "123qwe,.", required = true),
@ApiImplicitParam(name = "groupid", value = "组类别", dataType = "string", paramType = "query", example = "1001", required = true),
@ApiImplicitParam(name = "memo", value = "备注", dataType = "string", paramType = "query", example = "...", required = true),
@ApiImplicitParam(name = "enable", value = "状态", dataType = "bit", paramType = "query", example = "1", required = true)
})
public int updateUser(User user){
int iRet= 0;
SqlSession sqlSession= null;
try{
sqlSession= MybatisUtils.getSqlSession();
UserMapper mapper= sqlSession.getMapper(UserMapper.class);
iRet= mapper.updateUser(user);
} catch (Exception e){
e.printStackTrace();
} finally {
if (sqlSession!= null){
sqlSession.close();
}
}
return iRet;
}
@PostMapping("/user/deleteUser")
@ApiOperation(value = "删除特定账号信息",notes = "删除特定账号信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "username", value = "账号", dataType = "string", paramType = "query", example = "10086", required = true)
})
public int deleteUser(String username){
int iRet= 0;
SqlSession sqlSession= null;
try{
sqlSession= MybatisUtils.getSqlSession();
UserMapper mapper= sqlSession.getMapper(UserMapper.class);
iRet= mapper.deleteUser(username);
} catch (Exception e){
e.printStackTrace();
} finally {
if (sqlSession!= null){
sqlSession.close();
}
}
return iRet;
}
@PostMapping("/user/addUser")
@ApiOperation(value = "新增账号信息",notes = "新增账号信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "username", value = "账号", dataType = "string", paramType = "query", example = "10086", required = true),
@ApiImplicitParam(name = "password", value = "密码", dataType = "string", paramType = "query", example = "123qwe,.", required = true),
@ApiImplicitParam(name = "groupid", value = "组类别", dataType = "string", paramType = "query", example = "1001", required = true),
@ApiImplicitParam(name = "memo", value = "备注", dataType = "string", paramType = "query", example = "...", required = true),
@ApiImplicitParam(name = "enable", value = "状态", dataType = "bit", paramType = "query", example = "1", required = true)
})
public int addUser(User user){
int iRet= 0;
SqlSession sqlSession= null;
try{
sqlSession= MybatisUtils.getSqlSession();
UserMapper mapper= sqlSession.getMapper(UserMapper.class);
iRet= mapper.addUser(user);
} catch (Exception e){
e.printStackTrace();
} finally {
if (sqlSession!= null){
sqlSession.close();
}
}
return iRet;
}
}
5. 运行效果