如何将MapStruct与Grails一起使用



在我的Grails项目中,我需要将对象从一种类型(通常是map(映射到另一个类型(一些POGO(。有一些很好的例子可以在Boot-Spring中实现这一点,但Grails的例子不多。

我在build.gradle:中添加了以下行

compile 'org.mapstruct:mapstruct:1.4.2.Final'
compileOnly 'org.mapstruct:mapstruct-processor:1.4.2.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'
...
compileJava {
options.annotationProcessorPath = configurations.annotationProcessor
// if you need to configure mapstruct component model
options.compilerArgs << "-Amapstruct.defaultComponentModel=spring"
}

我有一个映射器定义为:

@Mapper(componentModel = "spring")
public interface AuthResponseMapper2
{
@Mapping(source = "access_token", target = "token")
@Mapping(source = "token_type", target = "type")
GetTokenResponse toGetTokenResponse(Map map);
}

然而,我没有看到为该映射程序生成的类;当我尝试使用它时,我会得到一个NoSuchBeanDefinitionException。不知道从这里去哪里。

最好检查一个AuthResponseMapper2Impl类是否由MapStruct生成。如果是这样的话,你需要确保Spring Boot扫描了包,并将其作为一个组件。

除了目前不支持从Map映射到POJO之外,您还可以按照mapstruct/mapstruct#1072进行此功能请求

最新更新