-
카테고리
-
세부 분야
백엔드
-
해결 여부
미해결
Entity에 setter 사용 안할시 ModelMapper 설정
23.01.11 21:00 작성 23.01.11 21:02 수정 조회수 672
2
수강생분들에게 정보 공유해드리고 싶어서 게시판에 글 남깁니다.
ModelMapper
를 사용하면 객체에서 객체로 쉽게 변환할 수 있습니다.
그런데 Setter
를 사용하지 않은 Entity
또는 객체
에서 Dto
로 변환할 매핑되지 않는 문제가 발생합니다.
이를 해결하기 위해서
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setFieldAccessLevel(Configuration.AccessLevel.PRIVATE)
.setFieldMatchingEnabled(true);
위와 같이 설정한뒤 사용하면 됩니다.^^
저는
@Bean
public ModelMapper modelMapper() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setFieldAccessLevel(Configuration.AccessLevel.PRIVATE)
.setFieldMatchingEnabled(true);
return modelMapper;
}
빈으로 등록해서 사용했습니다.ㅎㅎ
InheritingConfiguration
클래스에서 ModelMapper
기본 fieldAccessLevel
이 fieldAccessLevel = AccessLevel.PUBLIC;
임을 확인 할 수 있습니다.^^
public class InheritingConfiguration implements Configuration {
//...//
/**
* Creates an initial InheritingConfiguration.
*/
public InheritingConfiguration() {
parent = null;
typeMapStore = new TypeMapStore(this);
converterStore = new ConverterStore();
valueAccessStore = new ValueAccessStore();
valueMutateStore = new ValueMutateStore();
sourceNameTokenizer = NameTokenizers.CAMEL_CASE;
destinationNameTokenizer = NameTokenizers.CAMEL_CASE;
sourceNamingConvention = NamingConventions.JAVABEANS_ACCESSOR;
destinationNamingConvention = NamingConventions.JAVABEANS_MUTATOR;
sourceNameTransformer = NameTransformers.JAVABEANS_ACCESSOR;
destinationNameTransformer = NameTransformers.JAVABEANS_MUTATOR;
matchingStrategy = MatchingStrategies.STANDARD;
fieldAccessLevel = AccessLevel.PUBLIC;
methodAccessLevel = AccessLevel.PUBLIC;
fieldMatchingEnabled = Boolean.FALSE;
ambiguityIgnored = Boolean.FALSE;
fullTypeMatchingRequired = Boolean.FALSE;
implicitMatchingEnabled = Boolean.TRUE;
preferNestedProperties = Boolean.TRUE;
skipNullEnabled = Boolean.FALSE;
useOSGiClassLoaderBridging = Boolean.FALSE;
collectionsMergeEnabled = Boolean.FALSE;
}
//...//
}
참고하세용^^
답변을 작성해보세요.
답변 1