1.加入依赖

  1. <!--swagger-->
  2. <dependency>
  3. <groupId>io.springfox</groupId>
  4. <artifactId>springfox-swagger2</artifactId>
  5. <version>2.9.2</version>
  6. <exclusions>
  7. <exclusion>
  8. <groupId>io.swagger</groupId>
  9. <artifactId>swagger-annotations</artifactId>
  10. </exclusion>
  11. <exclusion>
  12. <groupId>io.swagger</groupId>
  13. <artifactId>swagger-models</artifactId>
  14. </exclusion>
  15. </exclusions>
  16. </dependency>
  17. <dependency>
  18. <groupId>io.springfox</groupId>
  19. <artifactId>springfox-swagger-ui</artifactId>
  20. <version>2.9.2</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>io.swagger</groupId>
  24. <artifactId>swagger-annotations</artifactId>
  25. <version>1.5.21</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>io.swagger</groupId>
  29. <artifactId>swagger-models</artifactId>
  30. <version>1.5.21</version>
  31. </dependency>

2.加入SwaggerConfig配置类

  1. package com.xlwy.fkudp.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import springfox.documentation.builders.ApiInfoBuilder;
  5. import springfox.documentation.builders.PathSelectors;
  6. import springfox.documentation.builders.RequestHandlerSelectors;
  7. import springfox.documentation.service.ApiInfo;
  8. import springfox.documentation.service.Contact;
  9. import springfox.documentation.spi.DocumentationType;
  10. import springfox.documentation.spring.web.plugins.Docket;
  11. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  12. @Configuration
  13. @EnableSwagger2
  14. public class SwaggerConfig {
  15. @Bean
  16. public Docket createRestApi() {
  17. return new Docket(DocumentationType.SWAGGER_2)
  18. //apiInfo指定测试文档基本信息,这部分将在页面展示
  19. .apiInfo(apiInfo())
  20. .select()
  21. //apis() 控制哪些接口暴露给swagger,
  22. // RequestHandlerSelectors.any() 所有都暴露
  23. // RequestHandlerSelectors.basePackage("com.info.*") 指定包位置
  24. .apis(RequestHandlerSelectors.any())
  25. .paths(PathSelectors.any())
  26. .build();
  27. }
  28. //基本信息,页面展示
  29. private ApiInfo apiInfo() {
  30. return new ApiInfoBuilder()
  31. .title("PACUDP")
  32. .description("Prevention and control UDP 接收物联网设备数据报文")
  33. //联系人实体类
  34. .contact(
  35. new Contact("Prevention and control", "http://127.0.0.1:9000/swagger-ui.html#/", "邮箱")
  36. )
  37. //版本号
  38. .version("1.0.0-SNAPSHOT")
  39. .build();
  40. }
  41. }

3.Controller加入注解

  1. /**
  2. * Http调用udp客户端,向服务端发送数据
  3. * @author: WHN
  4. * @date: 2020/10/24 11:45
  5. * @param: [ ip:目标服务器ip, port:目标服务器端口, messageBody:将要发送的消息]
  6. * @return: void
  7. */
  8. @ApiOperation("Http调用udp客户端,向服务端发送数据")
  9. @ApiImplicitParams({
  10. @ApiImplicitParam(name = "ip", value = "ip", dataType = "String", paramType = "query",required = true,defaultValue = "127.0.0.1"),
  11. @ApiImplicitParam(name = "port", value = "port", dataType = "int", paramType = "query",required = true,defaultValue = "9000"),
  12. @ApiImplicitParam(name = "messageBody", value = "messageBody", dataType = "String", paramType = "query",required = true,defaultValue = "0000"),
  13. })
  14. @GetMapping("/clientSendMessage")
  15. public void sendMessage(String ip, int port, String messageBody){
  16. UdpClientConf udpClientConf = new UdpClientConf(ip, port, 5000);
  17. UdpClient udpClient = new UdpClient(udpClientConf);
  18. udpClient.start();
  19. udpClient.send(messageBody);
  20. Console.log("HTTP调用UdpClient向{}:{}发送数据:【{}】" + "\n", ip,port, messageBody);
  21. Console.log("客户端发送消息:" + messageBody);
  22. }

4.启动项目,进入网址,测试

测试地址:http://localhost:9000/swagger-ui.html#/