0

I'm getting this error when trying to start my Spring application. My classes are attached below but in a simplified manner.

The projet uses the pattern dto → service → serviceImpl → repository.

DentistDto.java

@Data
public class DentistDto {

    @NotBlank
    @Size(max = 11)
    private String croNumber;

    @Valid
    Person person;

}

DentistModel.java

@Data
@Entity
@Table(name = "dentists")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class DentistModel {

    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    @Column(name = "dentist_id", updatable = false, nullable = false)
    @Type(type = "org.hibernate.type.UUIDCharType")
    private UUID id;

    @Column(nullable = false, unique = true, length = 10)
    private String croNumber;

...

DentistController.java

    @CrossOrigin
    @RequestMapping("/dentists")
    @RestController
    public class DentistController {
    
         private final DentistService dentistService;
    
        public DentistController(DentistService dentistService) {
            this.dentistService = dentistService;
        }
    
        @PostMapping
        public ResponseEntity<Object> saveDentist(@RequestBody @Valid DentistDto dentistDto) {
            return dentistService.save(dentistDto);
        }
    
        @GetMapping
        public ResponseEntity<Object> getAllDentists() throws NotFoundException {
            return dentistService.findAll();
        }
...

DentistServiceImpl.java

    @Service
    public class DentistServiceImpl implements DentistService {
    
        private final DentistRepository dentistRepository;
        private final DentistMapper dentistMapper = DentistMapper.INSTANCE;
    
        public DentistServiceImpl(DentistRepository dentistRepository) {
            this.dentistRepository = dentistRepository;
        }
    
        @Override
        public DentistDto findById(UUID id) throws NotFoundException {
            var dentist = dentistRepository.findById(id).orElseThrow(() -> new NotFoundException());
            return dentistMapper.toDto(dentist);
        }
...

DentistService.java

public interface DentistService {

    DentistDto findById(UUID id) throws NotFoundException;

    ResponseEntity<Object> findAll() throws NotFoundException;

    ResponseEntity<Object> save(DentistDto dto);

...

DentistRepository.java

@Repository
public interface DentistRepository extends JpaRepository<DentistModel, UUID> {

    Optional<Object> findByCroNumber(String croNumber);
}

DentistMapper.java

import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

    @Mapper
    public interface DentistMapper {
    
        DentistMapper INSTANCE = Mappers.getMapper(DentistMapper.class);
    
        DentistModel toModel(DentistDto dto);
        DentistDto toDto(DentistModel model);
    }

Full error

24-11-2022 09:46:41.159 | 178 | [main] | INFO | com.api.lores.LoresApplication - Starting the Lores API 24-11-2022 09:46:41.312 | 331 | [Thread-0] | DEBUG | o.s.b.d.r.c.RestartClassLoader - Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@4fb091b 24-11-2022 09:46:41.315 | 334 | [restartedMain] | INFO | com.api.lores.LoresApplication - Starting the Lores API 24-11-2022 09:46:41.605 | 624 | [restartedMain] | INFO | com.api.lores.LoresApplication - Starting LoresApplication using Java 17.0.4.1 on DESKTOP-733E7TU with PID 13984 (C:\Users\Guilherme Lopes\repos\lores\target\classes started by Guilherme Lopes in C:\Users\Guilherme Lopes\repos\lores) 24-11-2022 09:46:41.606 | 625 | [restartedMain] | INFO | com.api.lores.LoresApplication - No active profile set, falling back to 1 default profile: "default" 24-11-2022 09:46:44.565 | 3584 | [restartedMain] | WARN | o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dentistController' defined in file [C:\Users\Guilherme Lopes\repos\lores\target\classes\com\api\lores\controller\DentistController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dentistServiceImpl' defined in file [C:\Users\Guilherme Lopes\repos\lores\target\classes\com\api\lores\service\dentist\DentistServiceImpl.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.api.lores.service.dentist.DentistServiceImpl]: Constructor threw exception; nested exception is java.lang.ExceptionInInitializerError 24-11-2022 09:46:44.599 | 3618 | [restartedMain] | ERROR | o.s.boot.SpringApplication - Application run failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dentistController' defined in file [C:\Users\Guilherme Lopes\repos\lores\target\classes\com\api\lores\controller\DentistController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dentistServiceImpl' defined in file [C:\Users\Guilherme Lopes\repos\lores\target\classes\com\api\lores\service\dentist\DentistServiceImpl.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.api.lores.service.dentist.DentistServiceImpl]: Constructor threw exception; nested exception is java.lang.ExceptionInInitializerError at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) at com.api.lores.LoresApplication.main(LoresApplication.java:17) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dentistServiceImpl' defined in file [C:\Users\Guilherme Lopes\repos\lores\target\classes\com\api\lores\service\dentist\DentistServiceImpl.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.api.lores.service.dentist.DentistServiceImpl]: Constructor threw exception; nested exception is java.lang.ExceptionInInitializerError at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:315) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:296) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1391) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ... 24 common frames omitted Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.api.lores.service.dentist.DentistServiceImpl]: Constructor threw exception; nested exception is java.lang.ExceptionInInitializerError at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:224) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117) at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:311) ... 38 common frames omitted Caused by: java.lang.ExceptionInInitializerError: null at com.api.lores.service.dentist.DentistServiceImpl.(DentistServiceImpl.java:23) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:211) ... 40 common frames omitted Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: Cannot find implementation for com.api.lores.mapper.DentistMapper at org.mapstruct.factory.Mappers.getMapper(Mappers.java:61) at com.api.lores.mapper.DentistMapper.(DentistMapper.java:10) ... 47 common frames omitted Caused by: java.lang.ClassNotFoundException: Cannot find implementation for com.api.lores.mapper.DentistMapper at org.mapstruct.factory.Mappers.getMapper(Mappers.java:75) at org.mapstruct.factory.Mappers.getMapper(Mappers.java:58) ... 48 common frames omitted

How can I fix this type of error?

guilhermxlopes
  • 138
  • 1
  • 7
  • 2
    Please add the full stacktrace instead of a snippet. – M. Deinum Nov 24 '22 at 12:53
  • @M.Deinum done, I just edited the post. – guilhermxlopes Nov 24 '22 at 12:59
  • 2
    Please add the dentist related entity and controller class. – Hamza Khadhri Nov 24 '22 at 13:15
  • @HamzaKhadhri my mistake, I just added the DentistController and DentistModel classes. – guilhermxlopes Nov 24 '22 at 13:19
  • 2
    `Cannot find implementation for com.api.lores.mapper.DentistMapper` looks pretty clear to me. I suspect you are using MapStruct and haven't setup compilation correctly. – M. Deinum Nov 24 '22 at 13:26
  • According to the stacktrace there is also an error in DentistMapper. Could you add it? It is suspicious that is added via INSTANCE reference - I believe autowiring was expected – Oleg Kondrashov Nov 24 '22 at 13:28
  • @OlegKondrashov just added the DentistMapper, I do believe that all dentist related classes are posted now. – guilhermxlopes Nov 24 '22 at 13:29
  • @M.Deinum just added the DentistMapper, my mistake again. – guilhermxlopes Nov 24 '22 at 13:29
  • I believe you are doing mapping in a wrong way - you should delete a line with Instance initiation in DentistMapper class, add @Mapper(componentModel = "spring") annotation above this interface and inject it as a bean in a place you need. There is a guide you can refer to - https://www.baeldung.com/mapstruct – Oleg Kondrashov Nov 24 '22 at 13:33
  • 1
    You should also add your DentistDto class to check that mapping via mapstruct can be done correctly – Oleg Kondrashov Nov 24 '22 at 13:38
  • @OlegKondrashov just added the DentistDto too. – guilhermxlopes Nov 24 '22 at 13:40
  • I wasn't telling you to add that class. The stacktrace tells uyou what is wrong. Tehre is no implementation for that interface. You are using mapstruct and you have that setup wrong and thus it won't generate an implementation and thus your application fails to strt. – M. Deinum Nov 24 '22 at 13:53

2 Answers2

1

Uh, that stacktrace is badly mangled. It looks like you copied this from a console window with low line width that added plenty of line breaks, then used stack overflow's quote feature to eat all linebreaks. You can obtain a less mangled stack trace by logging to a file rather than a console, or using a console with much larger line width.

Having done that, the stack trace clearly tells you:

Caused by: java.lang.ClassNotFoundException: Cannot find implementation for com.api.lores.mapper.DentistMapper
    at org.mapstruct.factory.Mappers.getMapper(Mappers.java:75)
    at org.mapstruct.factory.Mappers.getMapper(Mappers.java:58) 
    ... 48 common frames omitted

(in Java, the root cause of an error is usually the last exception in the stack trace)

Now that you know which exception to google, the answer should be much closer at hand :-)

meriton
  • 68,356
  • 14
  • 108
  • 175
  • Thank you so much. I added `@ComponentScan(basePackages = {"com.api.lores.mapper"})` to my `Application`class and it worked. I do believe using this annotation I specify the `mapper` package and Spring can managed, right? – guilhermxlopes Nov 24 '22 at 13:56
-1

Try to add @Service to your DentistService, Like this:

@Service
public interface DentistService {

    DentistDto findById(UUID id) throws NotFoundException;

    ResponseEntity<Object> findAll() throws NotFoundException;

    ResponseEntity<Object> save(DentistDto dto);

}
Hamza Khadhri
  • 207
  • 4
  • 19