简体   繁体   中英

Springboot App: Consider defining a bean of type in your configuration

Im trying to start a springboot application. But when it's starting, i get an error message:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 1 of constructor in spring_boot.controller.RekeningController required a bean of type 'spring_boot.mappers.RekeningMapper' that could not be found.

Action:
Consider defining a bean of type 'spring_boot.mappers.RekeningMapper' in your configuration.

I tried to add the @Bean annotation in the RekeningMapper, but thats works not as wel. I search everywhere on the internet but i can't find a solution.

Below I have placed the code that it affects.

RekeningMapper

package spring_boot.mappers;

//Imports

@Mapper(componentModel = "spring")
public interface RekeningMapper {

    RekeningDto naarRekeningDto(Rekening rekening);
    Rekening naarRekeningModel(RekeningDto rekeningDto);
    
    @Mapping(source = "rekeningen", target = "rekeningenIDs")
    RekeningHouderDto naarRekeningHouderDto(RekeningHouder rekeningHouder);
    RekeningHouder naarRekeningHouderModel(RekeningHouderDto rekeningHouderDto);
    
    default List<String> rekeningToRekeningIds(List<Rekening> rekeningen) {
        List<String> ids = new ArrayList<>();
        
        for (Rekening rek : rekeningen)
            ids.add(rek.getRekening_id());
        
        return ids;
    }
}

RekeningController

package spring_boot.controller;

// Imports

@RestController
@RequestMapping("rekeningen")
public class RekeningController {
    
    private RekeningService rekeningService;
    private final RekeningMapper rekeningMapper;
    
    @Autowired
    public RekeningController(RekeningService rekeningService, RekeningMapper rekeningMapper) {
        this.rekeningMapper = rekeningMapper;
        this.rekeningService = rekeningService;
    }

    @GetMapping
    public ResponseEntity<List<RekeningDto>> getAllRekeningen() {
        List<Rekening> rekeningen = this.rekeningService.getAllRekeningen();
        List<RekeningDto> rekeningDtos = new ArrayList<>();
        
        for (Rekening rekening : rekeningen) 
            rekeningDtos.add(rekeningMapper.naarRekeningDto(rekening));
        
        return ResponseEntity.ok(rekeningDtos);
    }
}
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
    
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.0.Final</version>
    </dependency>

Your dependencies and mapstruct processor plugin need to look like this. Update the source and target to the java version you are using. Also make sure that your project is using Java 1.8 or later (project properties → "Java Compiler" → "Compile Compliance Level"). It will not work with older versions.

<properties>
    <org.mapstruct.version>1.4.0.CR1</org.mapstruct.version>
</properties>
<dependencies>
   <dependency>
       <groupId>org.mapstruct</groupId>
       <artifactId>mapstruct</artifactId>
       <version>${org.mapstruct.version}</version>
   </dependency>
</dependencies>

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.1</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <annotationProcessorPaths>
          <path>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${org.mapstruct.version}</version>
          </path>
        </annotationProcessorPaths>
     </configuration>
   </plugin>
 </plugins>
</build>

Furthermore To double check that everything is working as expected, go to your project's properties and select "Java Compiler" → "Annotation Processing" → "Factory Path". The MapStruct processor JAR should be listed and enabled there

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM