๐ ์๋ฌ ์ค๋ช
class java.util.LinkedHashMap cannot be cast to class com.XXXX.
XXXX.app.interfaces.devices.service.dto.StatusDto
๐ java.util.LinkedHashMap์ StatusDto ๊ฐ์ฒด๋ก ๊ฐ์ ๋ก ์บ์คํ ํ๋ ค ํ ๋ ๋ฐ์ํ๋ ClassException์ด๋ค.
Java์์ ํ์ ๋ถ์ผ์น๋ก ์ธํด ๋ฐ์ํ๋๊ฑด๋ฐ, ์ ์ด๋ฌํ ์ํฉ์ด ๋ฐ์ํ๋์ง ์์๋ณด์...
์๋ฌ๊ฐ ๋ฐ์ํ๋ ์ํฉ
๐ถ ์์ ์ ์ฝ๋
@Override
public Flux<Dto> getAll() {
return deviceRepository.findByXXXX()
.switchIfEmpty(Mono.error(new IllegalArgumentException("์ฌ์ฉ์์ ๋๋ฐ์ด์ค๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")))
.flatMapMany(device -> {
Object attributes = device.getStatus().get("attributes");
Object operations = device.getStatus().get("operations");
List<StatusDto> deviceAttributes = new ArrayList<>();
deviceAttributes.addAll((List<StatusDto>) attributes);
deviceAttributes.addAll((List<StatusDto>) operations);
log.info("deviceAttributes {}", deviceAttributes);
return repository.findAll()
.flatMap(mode -> {
Dto dto = new Dto();
for(StatusDto statusDto : wallPadAttributes) {
if (statusDto.getCode().equals(mode.getCode()))
dto.setValue(statusDto.getValue());
}
return Flux.just(dto);
});
});
});
}
- deviceRepository๋ฅผ ํตํด ์กฐํํ Device์ ํ๋ ์ค Map<String, Object> status ํ๋๊ฐ ์กด์ฌํ๋ค.
- DB์ Device์ status๊ฐ ์๋์ ๊ฐ์ด ์ ์ฅ๋์ด์๋ค.
{
"status": {
"attributes": [
{ "code": "outgoing", "value": "event" },
{ "code": "in", "value": "run" }
],
"operations" : [
{ "code": "out", "value": "run" },
]
}
}
device.getStatus().get("attributes")
์ ๋ฐํํ์ ์ Object์ด๊ธฐ ๋๋ฌธ์List<StatusDto> attributes = (List<StatusDto>) device.getStatus().get("attributes")
๋ก ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ์์์ ์บ์คํ ์ด ์ ๋ ๊ฑฐ๋ผ ์๊ฐํ๋ค.- ํ์ง๋ง getClass()๋ฅผ ์ด์ฉํด ํ์ ์ ํ์ธํ ๊ฒฐ๊ณผ, List<StatusDto> ํ์ ์ด ์๋๋ผ List<LinkedHashMap>์ด์๋ค.
๐ ์ Object๊ฐ LinkedHashMap์ด ๋๋๊ฑธ๊น ? ๐ค
MongoDB์์์ JSON ์ง๋ ฌํ
MongoDB์ ๊ฐ์ NoSQL DB์์๋ ์์ status ํ๋๋ฅผ JSON ํ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ค.
Java์์ JSON ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃฐ ๋ ์๋์ผ๋ก Map<String, Object> ํํ๋ก ๋ณํ๋๋ค.
{
"status": {
"attributes": [
{ "code": "outgoing", "value": "event" },
{ "code": "in", "value": "run" }
],
"operations" : [
{ "code": "out", "value": "run" },
]
}
}
์์ ๋ฐ์ดํฐ๋ฅผ Java์์ ์ฝ์ผ๋ฉด ์๋์ ๊ฐ์ด ๋งคํ๋๋ค.
Map<String, Object> status = new HashMap<>();
status.put("attributes", List.of(
Map.of("code", "outgoing", "value", "event"),
Map.of("code", "in", "value", "run")
));
status.put("operations", List.of(
Map.of("code", "out", "value", "run")
));
List<?> attributesList = (List<?>) attributes;
System.out.println(attributesList.get(0).getClass());
// ์ถ๋ ฅ๊ฒฐ๊ณผ
// >>> class java.util.LinkedHashMap
ํด๊ฒฐ๋ฐฉ๋ฒ
List<Map<String, Object>> ๐ List<StatusDto>๋ก ๋ณํํ๋ ๊ณผ์ ์ ์ถ๊ฐํด์ผํ๋ค.
private StatusDto mapToStatusDto(Map<String, Object> map) {
StatusDto statusDto = new StatusDto();
statusDto.setCode((String) map.get("code"));
statusDto.setValue(map.get("value"));
return statusDto;
}
์์ ํ ์ฝ๋
@Override
public Flux<Dto> getAll(String username) {
return deviceRepository.findByXXXX()
.switchIfEmpty(Mono.error(new IllegalArgumentException("์ฌ์ฉ์์ ๋๋ฐ์ด์ค๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")))
.flatMapMany(device -> {
Object attributes = device.getStatus().get("attributes");
Object operations = device.getStatus().get("operations");
log.info("attributes : {}", attributes);
log.info("operations : {}", operations);
List<StatusDto> deviceAttributes = new ArrayList<>();
// Map์ StatusDto๋ก ๋ณํ
for (Object attr : (List<?>) attributes) {
deviceAttributes.add(mapToStatusDto((Map<String, Object>) attr));
}
for (Object op : (List<?>) operations) {
deviceAttributes.add(mapToStatusDto((Map<String, Object>) op));
}
return repository.findAll()
.flatMap(mode -> {
Dto dto = new Dto();
for(StatusDto statusDto : deviceAttributes) {
if (statusDto.getCode().equals(mode.getCode()))
dto.setValue(statusDto.getValue());
}
return Flux.just(dto);
});
});
});
}