Swagger2怎么在春季启动项目中使用

  介绍

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

<强>添加Swagger2依赖

在pom。xml中加入Swagger2的依赖

& lt; dependency>   & lt;才能groupId> io.springfox</groupId>   & lt;才能artifactId> springfox-swagger2</artifactId>   & lt;才能version> 2.2.2</version>   & lt;/dependency>   & lt; dependency>   & lt;才能groupId> io.springfox</groupId>   & lt;才能artifactId> springfox-swagger-ui</artifactId>   & lt; version> 2.2.2   & lt;/dependency>

<强>创建Swagger2配置类

在应用程序。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 摘要(DocumentationType.SWAGGER_2)   ,,,,,,,.apiInfo (apiInfo ())   ,,,,,,,.select ()   ,,,,,,,.apis (RequestHandlerSelectors.basePackage(“你自己的外部接口包名称“))   ,,,,,,,.paths (PathSelectors.any ())   ,,,,,,,.build ();   ,,}      private 才能;ApiInfo  apiInfo (), {   ,,,return  new  ApiInfoBuilder ()   ,,,,,,,.title(“词网Neo4j  RESTful  APIs")   ,,,,,,,.description (“, Neo4j  RESTful  APIs 描述/?   ,,,,,,,.termsOfServiceUrl (“”)   ,,,,,,,.contact(“李庆海“)   ,,,,,,,.version (“5.0”)   ,,,,,,,.build ();   ,,}   }

<>强添加文档内容

在完成了上述配置后,其实已经可以生产文档内容,但是这样的文档主要针对请求本身,而描述主要来源于函数等命名产生,对用户并不友好,我们通常需要自己增加一些说明来丰富文档内容。

import  io.swagger.annotations.Api;   import  io.swagger.annotations.ApiOperation;   import  io.swagger.annotations.ApiParam;/* *   ,*系统用户控制器   *大敌;   ,* @author 李庆海   ,*   ,*/@Api (value =,“系统用户接口,,,tags =,“系统管理“)   @RestController   @RequestMapping (“/v3/edu/users")   public  class  UserController  {      @ autowired才能   private 才能;UserService  userService;      ,/* *   ,,*,添加用户,注册   ,,*,   ,,*,@param  loginName   *,,,,,,,,登录账号   ,,*,@param 用户名   *,,,,,,,,用户名称   ,,*,@param 密码   *,,,,,,,,登录密码   ,,*,@param  roleId   *,,,,,,,,用户角色   ,,* @return   ,,*,@throws  ResourceExistsException   ,,*/@ApiOperation才能(value =,“添加用户“)   @PostMapping才能(“/?   public 才能;JsonResult 创建(   ,,,,,@ApiParam (=name “loginName",, value =,“登录账号“,,required =,真的),@RequestParam (required =, true), @RequestBody  String  loginName,   ,,,,,@ApiParam (=name “userName",, value =,“用户名称“,,required =,真的),@RequestParam (required =, true), @RequestBody  String 用户名、   ,,,,,@ApiParam (=name “password",, value =,“登录密码“、,required =,真的),@RequestParam (required =, true), @RequestBody  String 密码,   ,,,,,@ApiParam (=name “roleId",, value =,“用户角色编号“,,required =,真的),@RequestParam (required =, true), @RequestBody  String  roleId)   ,,,,,throws  ResourceExistsException  {   null   null   null   null   null   null   null   null

Swagger2怎么在春季启动项目中使用