Skip to content

Commit 2df0866

Browse files
authored
유저 스킬, 포지션 빈값 response 할시 500 오류 해결
유저 회원가입 후에는 스킬, 포지션 빈값으로 받게되는데 본인의 스킬,포지션 수정을 안하고 본인 정보를 조회시 response에 500 오류가 나는 현상이 있어 수정했습니다
2 parents 6724c87 + f36e9be commit 2df0866

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
11
package hs.kr.backend.devpals.domain.user.convert;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import jakarta.persistence.AttributeConverter;
45
import jakarta.persistence.Converter;
56
import com.fasterxml.jackson.core.type.TypeReference;
67
import com.fasterxml.jackson.databind.ObjectMapper;
78

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

1012
@Converter
1113
public class LongListConverter implements AttributeConverter<List<Long>, String> {
1214

13-
private static final ObjectMapper objectMapper = new ObjectMapper();
15+
private final ObjectMapper objectMapper = new ObjectMapper();
1416

1517
@Override
1618
public String convertToDatabaseColumn(List<Long> attribute) {
19+
if (attribute == null || attribute.isEmpty()) {
20+
return "[]";
21+
}
1722
try {
1823
return objectMapper.writeValueAsString(attribute);
19-
} catch (Exception e) {
24+
} catch (JsonProcessingException e) {
2025
throw new RuntimeException("JSON 변환 오류", e);
2126
}
2227
}
2328

2429
@Override
2530
public List<Long> convertToEntityAttribute(String dbData) {
31+
if (dbData == null || dbData.trim().isEmpty()) {
32+
return new ArrayList<>(); // 빈 값일 경우 빈 리스트 반환
33+
}
2634
try {
27-
return objectMapper.readValue(dbData, new TypeReference<List<Long>>() {});
28-
} catch (Exception e) {
35+
return objectMapper.readValue(dbData, new TypeReference<>() {});
36+
} catch (JsonProcessingException e) {
2937
throw new RuntimeException("JSON 변환 오류", e);
3038
}
3139
}
32-
}
40+
}

src/main/java/hs/kr/backend/devpals/domain/user/dto/UserResponse.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.time.LocalDateTime;
1010
import java.util.List;
11+
import java.util.Optional;
1112
import java.util.stream.Collectors;
1213

1314
@Getter
@@ -27,8 +28,8 @@ public class UserResponse {
2728
private LocalDateTime createdAt;
2829

2930
public static UserResponse fromEntity(UserEntity user, UserFacade userFacade) {
30-
List<Long> positionIds = user.getPositionIds();
31-
List<Long> skillIds = user.getSkillIds();
31+
List<Long> positionIds = Optional.ofNullable(user.getPositionIds()).orElse(List.of());
32+
List<Long> skillIds = Optional.ofNullable(user.getSkillIds()).orElse(List.of());
3233

3334
List<PositionTagResponse> positionResponses = userFacade.getPositionTagByIds(positionIds).stream()
3435
.map(PositionTagResponse::fromEntity)

src/main/java/hs/kr/backend/devpals/domain/user/entity/UserEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ public class UserEntity {
5656

5757
@Convert(converter = LongListConverter.class)
5858
@Column(columnDefinition = "TEXT")
59-
private List<Long> positionIds;
59+
private List<Long> positionIds = new ArrayList<>();
6060

6161
@Convert(converter = LongListConverter.class)
6262
@Column(columnDefinition = "TEXT")
63-
private List<Long> skillIds;
63+
private List<Long> skillIds = new ArrayList<>();
6464

6565
/*
6666
@OneToMany(mappedBy = "user")

0 commit comments

Comments
 (0)