I am using mapstruct to transform a DTO into an object and vice versus and I am getting the following exception:
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. My code is on github on this branch 42_RenameCodeBaseToCustomerOnlinePortal. The code is pretty simple and not many lines of code. The exception is generated as part of the RegistrationEndpointIT.java
.
Please could you take a look where I am going wrong? It is using a gradle wrapper.
Additionally, I get the following exception when running 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.
Action:
Consider defining a bean of type 'com.rppjs.customer.online.portal.dtos.mapper.UserMapper' in your configuration.
Please note, Application.java is a Spring boot application.
The problem is that RegistrationEndpoint
uses the mapper as constructor argument. Since it is a component Spring wants to autowire it. But neither UserMapper
nor UserMapperImpl
are spring beans, therefore the exceptions.
You have two options:
Remove the UserMapper
constructor argument and get your mapper with Mappers.getMapper(UserMapper.class)
. Best practive would be to also public MAPPER
instance inside your mapper (see the example here )
If you need autowired dependencies inside your mapper you can define your mapper as as spring bean as follows:
@Mapper(componentModel = "spring")
@Component
public interface UserMapper() {
//...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.