springboot 中配置使用swagger2的方法(springboot,swagger2,开发技术)

时间:2024-05-04 20:34:32 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

1. maven依赖包

使用目前最新版本为例,pom.xml添加的代码如下

 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency>

2. 配置类的编写

配置类的编写同样非常简单,可以直接复制粘贴以下代码,但是一定要注意做适当修改,尤其是设置basePackage的路径,一定要根据实际情况修改。

新建一个config文件夹,在此文件夹中新建一个类

package cn.smileyan.swagger.config;import org.springframework.beans.factory.annotation.Configurable;import org.springframework.context.annotation.Bean;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;@EnableSwagger2@Configurablepublic class Swagger2 { /** * 特别要注意.apis(RequestHandlerSelectors.basePackage("cn.smileyan.swagger.controller")) * 此中的cn.smileyan.swagger.controller一定要修改为自己controller包。 * @return */ @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("cn.smileyan.swagger.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("springboot使用swagger例子") .description("简单优雅的restful风格") .termsOfServiceUrl("https://smileyan.cn") .version("1.0") .build(); }}

不能忘记类前面的@EnableSwagger2 与 @Configurable配置注解。以及后面的@Bean注解。

3. @EnableSwagger2 不能忘了

除了这个位置需要添加这个注解,还有springboot的运行类(application类)也要添加这个注释,否则会出现错误。

如图所示,我的application类名为SwaggerApplication,在这个类上面添加@EnableSwagger2

package cn.smileyan.swagger;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import springfox.documentation.swagger2.annotations.EnableSwagger2;@SpringBootApplication@EnableSwagger2public class SwaggerApplication { public static void main(String[] args) { SpringApplication.run(SwaggerApplication.class, args); }}

4. 编写controller类,添加注解,注意这个controller路径与上面配置类的路径要保持一致。

package cn.smileyan.swagger.controller;import io.swagger.annotations.ApiOperation;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;@RestController@RequestMapping("/user")public class UserController { @ApiOperation(value = "用户测试",notes = "贵宾用户") @RequestMapping(value = "",method = RequestMethod.GET) private Map<String,String> getUser() { Map<String,String> map = new HashMap<>(1); map.put("result","success"); return map; }}

5. 运行,打开api文档http://localhost:8080/swagger-ui.html

效果如下:

springboot 中配置使用swagger2的方法

可以点开user-controller,效果如下:

springboot 中配置使用swagger2的方法

常用注解

@Api : 修饰整个类,用于描述Controller类

@ApiOperation:描述类的方法,或者说一个接口

@ApiParam:单个参数描述

@ApiModel:用对象来接收参数

@ApiProperty:用对象接收参数时,描述对象的一个字段

@ApiResponse:HTTP响应的一个描述

@ApiResponses:HTTP响应的整体描述

@ApiIgnore:使用该注解,表示Swagger2忽略这个API

@ApiError:发生错误返回的信息

@ApiParamImplicit:一个请求参数

@ApiParamsImplicit:多个请求参数

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:springboot 中配置使用swagger2的方法的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Java实现批量导出word压缩后的zip文件下一篇:

5 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18