Swagger使用 使用总结

mac2024-06-02  45

Swagger是一个规范完整的框架

用于

1.接口文档自动生成

2.单元测试

一.Maven引入依赖包

Pom.xml 引入 swagger

<dependency>

    <groupId>io.springfox</groupId>

    <artifactId>springfox-swagger2</artifactId>

    <version>*.*</version>2.2.2

</dependency>

<dependency>

    <groupId>io.springfox</groupId>

    <artifactId>springfox-swagger-ui</artifactId>

    <version>*.*</version>

</dependency>

2.新建配置类

package com.niugang.config; package com.swaggerTest; 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; /** * Swagger2配置类 * 在与spring boot集成时,放在与Application.java同级的目录下。 * 通过@Configuration注解,让Spring来加载该类配置。 * 再通过@EnableSwagger2注解来启用Swagger2。 */ @Configuration @EnableSwagger2 public class Swagger2 { /** * 创建API应用 * apiInfo() 增加API相关信息 * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现, * 本例采用指定扫描的包路径来定义指定要建立API的目录。 * * @return */ @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.swaggerTest.controller"))//加载的包 .paths(PathSelectors.any()) .build(); } /** * 创建该API的基本信息(这些基本信息会展现在文档页面中) * 访问地址:http://项目实际地址/swagger-ui.html * @return */ private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot中使用Swagger构建RESTful APIs") .description("Swagger 学习") .termsOfServiceUrl("http://www.baidu.com") .contact("sunf") .version("1.0") .build(); } }

3.使用

Swagger使用的注解及其说明:

@Api:用在类上,说明该类的作用。

@ApiOperation:注解来给API增加方法说明。

@ApiImplicitParams : 用在方法上包含一组参数说明。

@ApiImplicitParam:用来注解来给方法入参增加说明。

@ApiResponses:用于表示一组响应

@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息

    l   code:数字,例如400

    l   message:信息,例如"请求参数没填好"

    l   response:抛出异常的类   

@ApiModel:描述一个Model的信息(一般用在请求参数无法使用@ApiImplicitParam注解进行描述的时候)

    l   @ApiModelProperty:描述一个model的属性

 

package com.niugang.controller; import java.util.concurrent.ExecutionException; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.kafka.support.SendResult; import org.springframework.util.concurrent.ListenableFuture; import org.springframework.util.concurrent.ListenableFutureCallback; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @Api(value = "SayController|一个用来测试swagger注解的控制器") public class SenderConttoller { private Logger logger = LoggerFactory.getLogger(SenderConttoller.class); @Autowired private KafkaTemplate<String, String> template; /** * 同步发送 * * @return * @throws ExecutionException * @throws InterruptedException */ @RequestMapping("syncSendMessage") @ApiOperation(value="syncSendMessage", notes="测试") public String syncSendMessage() { for (int i = 0; i < 100; i++) { try { template.send("kafka-boot", "0", "foo" + i).get(); } catch (InterruptedException e) { logger.error("sync send message fail [{}]", e.getMessage()); e.printStackTrace(); } catch (ExecutionException e) { logger.error("sync send message fail [{}]", e.getMessage()); e.printStackTrace(); } } return "success"; } /** * 异步发送 * * @return */ @RequestMapping("asyncSendMessage") public String sendMessageAsync() { for (int i = 0; i < 10; i++) { /** * <p> * SendResult:如果消息成功写入kafka就会返回一个RecordMetaData对象;result. * getRecordMetadata() 他包含主题信息和分区信息,以及集成在分区里的偏移量。 * 查看RecordMetaData属性字段就知道了 * </p> * */ ListenableFuture<SendResult<String, String>> send = template.send("kafka-boot", "0", "20191021测试发" + i); send.addCallback(new ListenableFutureCallback<SendResult<String, String>>() { @Override public void onSuccess(SendResult<String, String> result) { logger.info("async send message success partition [{}]", result.getRecordMetadata().partition()); logger.info("async send message success offest[{}]", result.getRecordMetadata().offset()); } @Override public void onFailure(Throwable ex) { logger.error("async send message fail [{}]", ex.getMessage()); } }); } return "success"; } }
最新回复(0)