现在前端传给我List两组数据,分别是主备设备的信息包括用户名密码ip端口,现在需要验证这两组数据,方法怎么写,java,controller层怎么写
时间: 2024-05-07 07:15:11 浏览: 51
可以先定义一个设备信息的类,包括用户名、密码、IP和端口:
```
public class DeviceInfo {
private String username;
private String password;
private String ip;
private int port;
public DeviceInfo(String username, String password, String ip, int port) {
this.username = username;
this.password = password;
this.ip = ip;
this.port = port;
}
// getter and setter methods
}
```
然后在Controller层中定义一个验证方法,接收两组设备信息的List作为参数,返回一个布尔值表示两组信息是否一致:
```
@RestController
@RequestMapping("/devices")
public class DeviceController {
@PostMapping("/validate")
public boolean validateDevices(@RequestBody List<DeviceInfo> primaryDevices, @RequestBody List<DeviceInfo> backupDevices) {
// 验证逻辑
}
}
```
在验证逻辑中,可以遍历两组设备信息,对比它们的每个字段是否一致。如果所有字段都一致,就返回true,否则返回false:
```
public boolean validateDevices(List<DeviceInfo> primaryDevices, List<DeviceInfo> backupDevices) {
if (primaryDevices.size() != backupDevices.size()) {
// 如果两组设备数量不一致,直接返回false
return false;
}
for (int i = 0; i < primaryDevices.size(); i++) {
DeviceInfo primaryDevice = primaryDevices.get(i);
DeviceInfo backupDevice = backupDevices.get(i);
if (!primaryDevice.getUsername().equals(backupDevice.getUsername())
|| !primaryDevice.getPassword().equals(backupDevice.getPassword())
|| !primaryDevice.getIp().equals(backupDevice.getIp())
|| primaryDevice.getPort() != backupDevice.getPort()) {
// 如果任意一个字段不一致,返回false
return false;
}
}
// 如果所有字段都一致,返回true
return true;
}
```
阅读全文