Swagger2怎么在Spring Boot 项目中使用?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站制作、网站建设、罗田网络推广、小程序制作、罗田网络营销、罗田企业策划、罗田品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供罗田建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com
添加Swagger2依赖
在pom.xml中加入Swagger2的依赖
io.springfox springfox-swagger2 2.2.2 io.springfox springfox-swagger-ui 2.2.2
创建Swagger2配置类
在Application.java同级创建Swagger2的配置类Swagger2。
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 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("你自己的外部接口包名称"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("词网Neo4j RESTful APIs")
.description("The Neo4j RESTful APIs description/")
.termsOfServiceUrl("")
.contact("李庆海")
.version("5.0")
.build();
}
}添加文档内容
在完成了上述配置后,其实已经可以生产文档内容,但是这样的文档主要针对请求本身,而描述主要来源于函数等命名产生,对用户并不友好,我们通常需要自己增加一些说明来丰富文档内容。
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
/**
* 系统用户Controller
*
* @author 李庆海
*
*/
@Api(value = "系统用户接口", tags = "系统管理")
@RestController
@RequestMapping("/v3/edu/users")
public class UserController {
@Autowired
private UserService userService;
/**
* 添加用户,注册
*
* @param loginName
* 登录账号
* @param userName
* 用户名称
* @param password
* 登录密码
* @param roleId
* 用户角色
* @return
* @throws ResourceExistsException
*/
@ApiOperation(value = "添加用户")
@PostMapping("/")
public JsonResult create(
@ApiParam(name = "loginName", value = "登录账号", required = true) @RequestParam(required = true) @RequestBody String loginName,
@ApiParam(name = "userName", value = "用户名称", required = true) @RequestParam(required = true) @RequestBody String userName,
@ApiParam(name = "password", value = "登录密码", required = true) @RequestParam(required = true) @RequestBody String password,
@ApiParam(name = "roleId", value = "用户角色编号", required = true) @RequestParam(required = true) @RequestBody String roleId)
throws ResourceExistsException {
boolean exists = this.userService.exists(loginName);
if (exists) {
throw new ResourceExistsException(loginName);
}
User user = userService.create(loginName, password, userName, roleId);
return new JsonResult(user);
}
}查看API
启动Spring Boot程序,访问:http://localhost:8080/swagger-ui.html

看完上述内容,你们掌握Swagger2怎么在Spring Boot 项目中使用的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!