java使用原生swagger_Spring Boot 项目中使用Swagger2的示例

本文介绍了Spring Boot 项目中使用Swagger2的示例,分享给大家,具体如下:

添加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

51c99af0b5b54bf3497508a55f3eb96f.png

API文档访问与调试

Swagger除了查看接口功能外,还提供了调试测试功能,我们可以点击上图中右侧的Model Schema(黄色区域:它指明了数据结构),此时Value中就有了user对象的模板,我们只需要稍适修改,点击下方Try it out!按钮,即可完成了一次请求调用!可以通过几个GET请求来验证之前的POST请求是否正确。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值