[英]Mapstruct cannot find impl
I am using mapstruct to transform a DTO into an object and vice versus and I am getting the following exception: 我正在使用mapstruct将DTO转换为对象,反之亦然,并且出现以下异常:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.rppjs.customer.online.portal.dtos.mapper.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1506)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:819)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:725)
I can see that UserMapper.impl
is being generated but still the above exception. 我可以看到正在生成
UserMapper.impl
,但仍然是上面的异常。 My code is on github on this branch 42_RenameCodeBaseToCustomerOnlinePortal. 我的代码在github上此分支42_RenameCodeBaseToCustomerOnlinePortal上。 The code is pretty simple and not many lines of code.
该代码非常简单,并且没有多少行代码。 The exception is generated as part of the
RegistrationEndpointIT.java
. 该异常是在
RegistrationEndpointIT.java
生成的。
Please could you take a look where I am going wrong? 请您看看我要去哪里了吗? It is using a gradle wrapper.
它正在使用gradle包装器。
Additionally, I get the following exception when running Application.java: 此外,运行Application.java时出现以下异常:
Description:
描述:
Parameter 0 of constructor in com.rppjs.customer.online.portal.endpoints.RegistrationEndpoint required a bean of type 'com.rppjs.customer.online.portal.dtos.mapper.UserMapper' that could not be found.
com.rppjs.customer.online.portal.endpoints.RegistrationEndpoint中的构造函数的参数0需要找不到类型为com.rppjs.customer.online.portal.dtos.mapper.UserMapper的bean。
Action:
行动:
Consider defining a bean of type 'com.rppjs.customer.online.portal.dtos.mapper.UserMapper' in your configuration.
考虑在配置中定义类型为“ com.rppjs.customer.online.portal.dtos.mapper.UserMapper”的bean。
Please note, Application.java is a Spring boot application. 请注意,Application.java是Spring引导应用程序。
The problem is that RegistrationEndpoint
uses the mapper as constructor argument. 问题在于
RegistrationEndpoint
使用映射器作为构造函数参数。 Since it is a component Spring wants to autowire it. 由于它是Spring想要自动装配的组件。 But neither
UserMapper
nor UserMapperImpl
are spring beans, therefore the exceptions. 但是
UserMapper
和UserMapperImpl
是spring bean,因此是例外。
You have two options: 您有两种选择:
Remove the UserMapper
constructor argument and get your mapper with Mappers.getMapper(UserMapper.class)
. 删除
UserMapper
构造函数参数,并使用Mappers.getMapper(UserMapper.class)
获取您的映射器。 Best practive would be to also public MAPPER
instance inside your mapper (see the example here ) 最好的方法是在您的映射器中也公开
MAPPER
实例(请参见此处的示例 )
If you need autowired dependencies inside your mapper you can define your mapper as as spring bean as follows: 如果需要在映射器内部自动关联依赖项,则可以将映射器定义为spring bean,如下所示:
@Mapper(componentModel = "spring")
@Component
public interface UserMapper() {
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.