提交 a3ada020 authored 作者: wangqiang's avatar wangqiang

rest test

上级 6114ea97
......@@ -8,10 +8,10 @@ import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, SecurityAutoConfiguration.class})
public class HaikangClientApplication {
public class ApqClientApplication {
public static void main(String[] args) {
SpringApplication.run(HaikangClientApplication.class, args);
SpringApplication.run(ApqClientApplication.class, args);
}
}
package com.priusis;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class HaikangClientApplicationTests {
@Test
void contextLoads() {
}
}
package com.priusis;
import cn.hutool.core.io.unit.DataSizeUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.system.oshi.OshiUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
import oshi.hardware.NetworkIF;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RestTest {
@Autowired
private RestTemplate restTemplate;
@Test
public void test1() throws Exception {
/*Map<String, Object> data = new HashMap<>();
// {
// "device": "101",
// "lat": "210.2",
// "lng": "11.21"
//}
data.put("device", "101");
data.put("lat", 210.2);
data.put("lng", 11.21);*/
InetAddress inetAddress = InetAddress.getLocalHost();
NetworkIF networkIF = new NetworkIF();
networkIF.setNetworkInterface(NetworkInterface.getByInetAddress(inetAddress));
Map<String, Object> data = MapUtil.<String, Object>builder()
.put("operatingSystem", OshiUtil.getOs().toString())
.put("mac", networkIF.getMacaddr())
.put("baseboard", OshiUtil.getSystem().getBaseboard().getManufacturer() + " " + OshiUtil.getSystem().getBaseboard().getVersion())
.put("ip", inetAddress.getHostAddress())
.put("cpuModel", OshiUtil.getCpuInfo(0).getCpuModel())
.put("cpuTemp", OshiUtil.getSensors().getCpuTemperature())
.put("memoryCap", DataSizeUtil.format(OshiUtil.getMemory().getTotal()))
.put("diskModel", OshiUtil.getHardware().getDiskStores()[0].getModel())
.put("diskCap", DataSizeUtil.format(OshiUtil.getHardware().getDiskStores()[0].getSize()))
.build();
Map map = restTemplate.postForObject("http://localhost:9090/uplink/oc-client", data, Map.class);
System.out.println(map);
}
}
......@@ -3,7 +3,12 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.10</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.priusis</groupId>
<artifactId>apq-pc-control</artifactId>
<version>1.0-SNAPSHOT</version>
......@@ -30,13 +35,23 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
<dependency>
......
package com.priusis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, SecurityAutoConfiguration.class})
public class ApqControlApplication {
public static void main(String[] args) {
SpringApplication.run(ApqControlApplication.class, args);
}
}
package com.priusis.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.Charset;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
RestTemplate restTemplate = new RestTemplate(factory);
// 支持中文编码
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(Charset.forName("UTF-8")));
return restTemplate;
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(5000);//单位为ms
factory.setConnectTimeout(5000);//单位为ms
return factory;
}
}
package com.priusis.vo;
import lombok.Data;
import java.io.Serializable;
@Data
public class MqttRpcDataMessage implements Serializable {
private static final long serialVersionUID = -3133461476074777891L;
private int requestId;
private String method;
private String params;
@Override
public String toString() {
return "{requestId=" + requestId +
", method=" + method +
", params=" + params +
'}';
}
}
server:
# Server bind address
address: "0.0.0.0"
port: ${random.int[20000,29999]}
package com.priusis;
import com.priusis.vo.MqttRpcDataMessage;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RestTest {
@Autowired
private RestTemplate restTemplate;
@Test
public void test1() throws Exception {
// 获取硬件监控配置 params == data
ResponseEntity<MqttRpcDataMessage> forEntity = restTemplate.getForEntity("http://localhost:8765/rpc_cmd/info", MqttRpcDataMessage.class);
System.out.println(forEntity.getBody().getMethod());
System.out.println(forEntity.getBody().getParams());
// 上报硬件监控数据
Map mapRequest = new HashMap();
Map map = restTemplate.postForObject("http://localhost:8765/uplink/oc-client", mapRequest, Map.class);
System.out.println(map);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论