2 回答

TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊
我應(yīng)該傳遞已經(jīng)在服務(wù)中轉(zhuǎn)換的標(biāo)題嗎?
你絕對(duì)不應(yīng)該這樣做。這是轉(zhuǎn)換器的工作,而不是服務(wù)的
嘗試以下方法:
1)枚舉類添加轉(zhuǎn)換方法
enum Title {
...
public static Title fromString(String title) {
if (title != null) {
for (Title t : Title.values()) {
if (t.toString().equals(title)) {
return t;
}
}
}
return null;
}
}
2) Mapper 接口增加2個(gè)轉(zhuǎn)換方法(僅限Java 8+)
@Mapper(componentModel = "spring")
public interface SignUpUserMapper {
SignUpUserDto signUpUserToSignUpUserDto(SignUpUser signUpUser);
SignUpUser signUpUserDtoToSignUpUser(SignUpUserDto signUpUserDto);
default String fromEnum(Title title) {
return title == null ? null : title.toString();
}
default Title toEnum(String title) {
return title == null ? null : Title.fromString(title);
}
}

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊
像這樣使用第二個(gè)選項(xiàng):
@Mapper(componentModel = "spring", imports = Title.class)
public interface SignUpUserMapper {
SignUpUserMapper INSTANCE = Mappers.getMapper(SignUpUserMapper.class);
@Mapping(target = "title", expression = "java(signUpUser.getTitle().toString())")
public SignUpUserDto signUpUserToSignUpUserDto(SignUpUser signUpUser);
@Mapping(target = "title", source = "java(Title.valueOf(signUpUserDto.getTitle().toUpperCase()))")
public SignUpUser signUpUserDtoToSignUpUser(SignUpUserDto signUpUserDto);
}
添加回答
舉報(bào)