关于枚举互相映射问题
前景: 我们是maven模块化管理,存在api提供给其他服务使用,此时我们需要将返回model与服务层的model进行剥离。
举例子 sql返回的对象A,A中包含枚举EnumA, 然后covertTo API层的model B, b中包含枚举Benum
然后在转换的时候使用了mapStruct来自动生成,方便改造。这里有片文章对mapStruct的使用介绍挺详细的。我使用的版本是1.4.1.final,
关于如何使用mapStruct
代码实现
pom
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<annotationProcessorPaths>
<!-- 此处,lombok依赖一定要放在,Mapstruct-processor依赖之前。否则,生成了maptruct的实现类,但该类只创建了对象,没有进行赋值 -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.2.Final</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
import org.mapstruct.Mapper;
@Mapper(componentModel = "spring")
public interface EnumMapper {
SourceTypeEnum toSourceTypeEnum(SourceType sourceType);
SourceType toSourceType(SourceTypeEnum enums);
GroupTypeEnum toGroupTypeEnum(GroupType groupType);
GroupType toGroupType(GroupTypeEnum groupTypeEnum);
UserTypeEnum toUserTypeEnum(UserType userType);
UserType toUserType(UserTypeEnum userTypeEnum);
}
@RequiredArgsConstructor
@Getter
public enum SourceTypeEnum implements EnumBase<String> {
LDAP("ldap"),
INNER("内部系统"),
WE_WORK("企业微信");
SourceTypeEnum(String message){
this.message = message;
this.code = this.name();
}
@EnumValue
private final String code;
private final String message;
}
@Getter
public enum SourceType implements EnumBase<String> {
LDAP("ldap"),
INNER("内部系统"),
WE_WORK("企业微信");
SourceType(String message){
this.message = message;
this.code = this.name();
}
private final String code;
private final String message;
}
以下为自动生成
package com.tarsocial.dag.auth.convert;
import com.tarsocial.dag.auth.enums.GroupTypeEnum;
import com.tarsocial.dag.auth.enums.SourceTypeEnum;
import com.tarsocial.dag.auth.enums.UserTypeEnum;
import com.tarsocial.dag.auth.model.enums.GroupType;
import com.tarsocial.dag.auth.model.enums.SourceType;
import com.tarsocial.dag.auth.model.enums.UserType;
import javax.annotation.Generated;
import org.springframework.stereotype.Component;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2023-06-07T15:43:21+0800",
comments = "version: 1.4.2.Final, compiler: javac, environment: Java 17.0.6 (Amazon.com Inc.)"
)
@Component
public class EnumMapperImpl implements EnumMapper {
@Override
public SourceTypeEnum toSourceTypeEnum(SourceType sourceType) {
if ( sourceType == null ) {
return null;
}
SourceTypeEnum sourceTypeEnum;
switch ( sourceType ) {
case LDAP: sourceTypeEnum = SourceTypeEnum.LDAP;
break;
case INNER: sourceTypeEnum = SourceTypeEnum.INNER;
break;
case WE_WORK: sourceTypeEnum = SourceTypeEnum.WE_WORK;
break;
default: throw new IllegalArgumentException( "Unexpected enum constant: " + sourceType );
}
return sourceTypeEnum;
}
@Override
public SourceType toSourceType(SourceTypeEnum enums) {
if ( enums == null ) {
return null;
}
SourceType sourceType;
switch ( enums ) {
case LDAP: sourceType = SourceType.LDAP;
break;
case INNER: sourceType = SourceType.INNER;
break;
case WE_WORK: sourceType = SourceType.WE_WORK;
break;
default: throw new IllegalArgumentException( "Unexpected enum constant: " + enums );
}
return sourceType;
}
@Override
public GroupTypeEnum toGroupTypeEnum(GroupType groupType) {
if ( groupType == null ) {
return null;
}
GroupTypeEnum groupTypeEnum;
switch ( groupType ) {
case ROLE: groupTypeEnum = GroupTypeEnum.ROLE;
break;
case DEPARTMENT: groupTypeEnum = GroupTypeEnum.DEPARTMENT;
break;
default: throw new IllegalArgumentException( "Unexpected enum constant: " + groupType );
}
return groupTypeEnum;
}
@Override
public GroupType toGroupType(GroupTypeEnum groupTypeEnum) {
if ( groupTypeEnum == null ) {
return null;
}
GroupType groupType;
switch ( groupTypeEnum ) {
case ROLE: groupType = GroupType.ROLE;
break;
case DEPARTMENT: groupType = GroupType.DEPARTMENT;
break;
default: throw new IllegalArgumentException( "Unexpected enum constant: " + groupTypeEnum );
}
return groupType;
}
@Override
public UserTypeEnum toUserTypeEnum(UserType userType) {
if ( userType == null ) {
return null;
}
UserTypeEnum userTypeEnum;
switch ( userType ) {
case INNER_USER: userTypeEnum = UserTypeEnum.INNER_USER;
break;
case NORMAL_USER: userTypeEnum = UserTypeEnum.NORMAL_USER;
break;
case SERVICE_USER: userTypeEnum = UserTypeEnum.SERVICE_USER;
break;
default: throw new IllegalArgumentException( "Unexpected enum constant: " + userType );
}
return userTypeEnum;
}
@Override
public UserType toUserType(UserTypeEnum userTypeEnum) {
if ( userTypeEnum == null ) {
return null;
}
UserType userType;
switch ( userTypeEnum ) {
case INNER_USER: userType = UserType.INNER_USER;
break;
case NORMAL_USER: userType = UserType.NORMAL_USER;
break;
case SERVICE_USER: userType = UserType.SERVICE_USER;
break;
default: throw new IllegalArgumentException( "Unexpected enum constant: " + userTypeEnum );
}
return userType;
}
}