문제가 생겼다..
현재 Enum 클래스는 아래와 같이 소문자를 value
필드로 저장하고 있다.
@Getter
@RequiredArgsConstructor
public enum AlbumType {
ALL("all"),
MAIN("main"),
INFANTS("infants")
private final String value;
public static AlbumType deserialize(String type){
for(AlbumType albumType : AlbumType.values()){
if(albumType.getValue().equalsIgnoreCase(type.trim())){
return albumType;
}
}
throw new AlbumException(AlbumExceptionCode.ILLEGAL_ALBUM_TYPE);
}
...
아래 사진과 같이 type필드에 소문자(value)로 저장되어 있다.
@Configuration
public class MongoConverterConfig {
@Bean
public MongoCustomConversions mongoCustomConversions() {
return new MongoCustomConversions(List.of(
// DB -> Enum
new Converter<String, AlbumType>() {
@Override
public AlbumType convert(String albumType) {
return AlbumType.deserialize(albumType);
}
},
....
public class RequestDto {
...
private String 다른필드1;
private String 다른필드2;
private String type;
...
다른필드1, 다른필드2
도내가 원하는 정확한 기능이였다.
이름으로도 쉽게 구분할 수 있었다!
[생성시]
요청 폼(String 소문자) → Enum 클래스 매핑(deseriaize 함수 사용)하여 엔티티 생성
→ DB에 소문자(Enum.value())로 저장 (이 때, 컨버터 사용됨)
[조회시]
DB(String 소문자) → Enum 클래스 매핑(컨버터 사용)하여 엔티티 생성
→ 응답에 소문자(Enum.getValue())
@Configuration
public class MongoConverterConfig {
@ReadingConverter
public static class stringToAlbumTypeConverter implements Converter<String, AlbumType> {
@Override
public AlbumType convert(String albumType){
return AlbumType.deserialize(albumType);
}
}
@WritingConverter
public static class albumTypeToStringConverter implements Converter<AlbumType, String> {
@Override
public String convert(AlbumType albumType) {
return albumType.getValue(); // enum 내부 커스텀 값
}
}
/**
* 커스컴 컨버터 Bean
*/
@Bean
public MongoCustomConversions mongoCustomConversions() {
return new MongoCustomConversions(List.of(
new stringToAlbumTypeConverter(),
new albumTypeToStringConverter()
));
}
....
stringToAlbumTypeConverter
albumTypeToStringConverter
mongoCustomConversions
해결!