'I am getting java.lang.ClassNotFoundException: Cannot find implementation for com.example.demo.mapper.DoctorMapper
This is a mapper interface for mapping:
@Mapper
public interface DoctorMapper {
DoctorMapper INSTANCE = Mappers.getMapper(DoctorMapper.class);
DoctorDto toDto(Doctor doctor);
}
This is a model class:
public class Doctor {
private int id;
private String name;
// getter and setter and constructors
}
This is a model class:
public class DoctorDto {
private int id;
private String name;
// getter and setter and constructors
}
Controller class:
@RestController
public class SimpleController {
@RequestMapping("/")
String hello() {
Doctor d= new Doctor(1,"Hari");
DoctorDto doctorDto = DoctorMapper.INSTANCE.toDto(d);
System.out.println(doctorDto.toString());
return "Hello World, Spring Boot!";
}
}
Any suggestion to map the doctor entity to doctordto? When I run the springboot application why do I encounter this issue?
java.lang.ClassNotFoundException: Cannot find implementation for com.example.demo.mapper.DoctorMapper
at org.mapstruct.factory.Mappers.getMapper(Mappers.java:75) ~[mapstruct-1.4.2.Final.jar:na]
at org.mapstruct.factory.Mappers.getMapper(Mappers.java:58) ~[mapstruct-1.4.2.Final.jar:na]
at com.example.demo.mapper.DoctorMapper.<clinit>(DoctorMapper.java:11) ~[classes/:na]
at com.example.demo.Controller.SimpleController.hello(SimpleController.java:18) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.18.jar:5.3.18]
at
Solution 1:[1]
You should not use "Mappers.get" in a spring boot context. Annotate your mapper with
componentModel = "spring",
and inject it like a normal bean.
private final DoctorMapper doctorMapper;
or
@Autowired
private DoctorMapper
Also make sure that you have not just the dependency for mapstruct in your project but also the dependency for the generation of the mapperImpls
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | GJohannes |
