Swagger2怎么在Spring Boot 项目中使用

发布时间:2021-03-29 17:15:19 作者:Leah
来源:亿速云 阅读:132

Swagger2怎么在Spring Boot 项目中使用?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

添加Swagger2依赖

在pom.xml中加入Swagger2的依赖

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.2.2</version>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>

创建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 项目中使用

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

推荐阅读:
  1. spring-boot-devtools怎么在spring-boot中使用
  2. 怎么在idea中创建Spring Boot项目

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot swagger2

上一篇:使用Node怎么对MongoDB数据库进行操作

下一篇:application.properties怎么在Spring Boot中使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》