在springboot 2 项目中使用Swagger2生成在线文档,项目启动时,出现大量类似以下日志
出现这种日志的主要原因是,两个不同的类 使用@ApiOperation注解,且不同的类中存在相同的方法名(enable)导致,如
// 类1,有以下代码
@RestController
@RequestMapping("/api/c1")
@Api(tags = "接口1")
public class C1Controller {
@ApiOperation(value = "启用记录", notes = "")
@ApiImplicitParam(paramType = "path", name = "id", value = "记录ID", required = true)
@PutMapping("/enable/{id}")
public HA<?> enable(@PathVariable Integer id){
try {
service.updateStatus(id);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 类2,也有以下代码
@RestController
@RequestMapping("/api/c2")
@Api(tags = "接口2")
public class C2Controller {
@ApiOperation(value = "启用记录", notes = "")
@ApiImplicitParam(paramType = "path", name = "id", value = "记录ID", required = true)
@PutMapping("/enable/{id}")
public HA<?> enable(@PathVariable Integer id){
try {
service.updateStatussssss(id);
} catch (Exception e) {
e.printStackTrace();
}
}
}
这时只要将类2或类1其中一个类的enable()方法名变更即可,只需修改方法名称。如,将类2的enable(),修改为enable2(),重启即可
// 类1,有以下代码
@RestController
@RequestMapping("/api/c1")
@Api(tags = "接口1")
public class C1Controller {
@ApiOperation(value = "启用记录", notes = "")
@ApiImplicitParam(paramType = "path", name = "id", value = "记录ID", required = true)
@PutMapping("/enable/{id}")
public HA<?> enable(@PathVariable Integer id){
try {
service.updateStatus(id);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 类2,也有以下代码
@RestController
@RequestMapping("/api/c2")
@Api(tags = "接口2")
public class C2Controller {
@ApiOperation(value = "启用记录", notes = "")
@ApiImplicitParam(paramType = "path", name = "id", value = "记录ID", required = true)
@PutMapping("/enable/{id}")
public HA<?> enable2(@PathVariable Integer id){
try {
service.updateStatussssss(id);
} catch (Exception e) {
e.printStackTrace();
}
}
}
本文介绍在Spring Boot项目中使用Swagger2时遇到的文档冲突问题,主要原因是不同控制器类中存在相同方法名导致Swagger无法正确生成文档。文章提供了解决方案,即更改其中一个类的方法名为不同的名称。
2万+

被折叠的 条评论
为什么被折叠?



