1.加入依赖
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.21</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.21</version>
</dependency>
2.加入SwaggerConfig配置类
package com.xlwy.fkudp.config;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
//apiInfo指定测试文档基本信息,这部分将在页面展示
.apiInfo(apiInfo())
.select()
//apis() 控制哪些接口暴露给swagger,
// RequestHandlerSelectors.any() 所有都暴露
// RequestHandlerSelectors.basePackage("com.info.*") 指定包位置
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
//基本信息,页面展示
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("PACUDP")
.description("Prevention and control UDP 接收物联网设备数据报文")
//联系人实体类
.contact(
new Contact("Prevention and control", "http://127.0.0.1:9000/swagger-ui.html#/", "邮箱")
)
//版本号
.version("1.0.0-SNAPSHOT")
.build();
}
}
3.Controller加入注解
/**
* Http调用udp客户端,向服务端发送数据
* @author: WHN
* @date: 2020/10/24 11:45
* @param: [ ip:目标服务器ip, port:目标服务器端口, messageBody:将要发送的消息]
* @return: void
*/
@ApiOperation("Http调用udp客户端,向服务端发送数据")
@ApiImplicitParams({
@ApiImplicitParam(name = "ip", value = "ip", dataType = "String", paramType = "query",required = true,defaultValue = "127.0.0.1"),
@ApiImplicitParam(name = "port", value = "port", dataType = "int", paramType = "query",required = true,defaultValue = "9000"),
@ApiImplicitParam(name = "messageBody", value = "messageBody", dataType = "String", paramType = "query",required = true,defaultValue = "0000"),
})
@GetMapping("/clientSendMessage")
public void sendMessage(String ip, int port, String messageBody){
UdpClientConf udpClientConf = new UdpClientConf(ip, port, 5000);
UdpClient udpClient = new UdpClient(udpClientConf);
udpClient.start();
udpClient.send(messageBody);
Console.log("HTTP调用UdpClient向{}:{}发送数据:【{}】" + "\n", ip,port, messageBody);
Console.log("客户端发送消息:" + messageBody);
}
4.启动项目,进入网址,测试
测试地址:http://localhost:9000/swagger-ui.html#/