Skip to content

Commit 5a3a434

Browse files
committed
Fix: StringListConverter에서 JSON 이중 인코딩 문제 해결
1 parent 0960135 commit 5a3a434

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed
Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,52 @@
11
package com.back.global.converter;
22

3-
import com.back.standard.util.Ut;
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
45
import jakarta.persistence.AttributeConverter;
56
import jakarta.persistence.Converter;
67
import org.springframework.stereotype.Component;
78

9+
import java.util.ArrayList;
810
import java.util.List;
911

1012
@Converter
1113
@Component
1214
public class StringListConverter implements AttributeConverter<List<String>, String> {
1315

16+
private static final ObjectMapper objectMapper = new ObjectMapper();
17+
1418
@Override
1519
public String convertToDatabaseColumn(List<String> attribute) {
16-
return attribute == null ? null : Ut.json.toString(attribute);
20+
if (attribute == null || attribute.isEmpty()) {
21+
return null;
22+
}
23+
try {
24+
return objectMapper.writeValueAsString(attribute);
25+
} catch (JsonProcessingException e) {
26+
throw new IllegalArgumentException("JSON 변환 실패", e);
27+
}
1728
}
1829

1930
@Override
2031
public List<String> convertToEntityAttribute(String dbData) {
21-
return Ut.json.toList(dbData, String.class);
32+
if (dbData == null || dbData.trim().isEmpty()) {
33+
return new ArrayList<>();
34+
}
35+
try {
36+
// 이중 인코딩된 경우 처리
37+
String data = dbData;
38+
if (data.startsWith("\"") && data.endsWith("\"")) {
39+
data = objectMapper.readValue(data, String.class);
40+
}
41+
42+
return objectMapper.readValue(
43+
data,
44+
objectMapper.getTypeFactory().constructCollectionType(List.class, String.class)
45+
);
46+
} catch (JsonProcessingException e) {
47+
System.err.println("JSON 파싱 실패. 원본 데이터: " + dbData);
48+
e.printStackTrace();
49+
return new ArrayList<>();
50+
}
2251
}
2352
}

0 commit comments

Comments
 (0)