Reputation: 457
I'm trying to develop an API using MapStruct in my "Category" controller, but the console is returning the error:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 1 of constructor in com.api.business_products_management.controllers.CategoryController required a bean of type 'com.api.business_products_management.mappers.CategoryMapper' that could not be found.
Action:
Consider defining a bean of type 'com.api.business_products_management.mappers.CategoryMapper' in your configuration.
Process finished with exit code 1
Here the controller
package com.api.business_products_management.controllers;
import com.api.business_products_management.dtos.CategoryDto;
import com.api.business_products_management.mappers.CategoryMapper;
import com.api.business_products_management.models.CategoryModel;
import com.api.business_products_management.services.CategoryService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.validation.Valid;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/category")
public class CategoryController {
final CategoryService categoryService;
final CategoryMapper categoryMapper;
public CategoryController(CategoryService categoryService, CategoryMapper categoryMapper) {
this.categoryService = categoryService;
this.categoryMapper = categoryMapper;
}
@PostMapping
public ResponseEntity<Object> saveCategory (@RequestBody @Valid CategoryDto categoryDto) {
if (categoryService.existsByName(categoryDto.getName())) {
return ResponseEntity.status(HttpStatus.CONFLICT).body("Conflict: Category already exists! ");
}
var categoryModel = categoryMapper.toCategoryModel(categoryDto);
return ResponseEntity.status(HttpStatus.CREATED).body(categoryService.save(categoryModel));
}
@GetMapping
public ResponseEntity<List<CategoryModel>> getAllCategories() {
return ResponseEntity.status(HttpStatus.OK).body(categoryService.findAll());
}
}
my CategoryMapper interface
package com.api.business_products_management.mappers;
import com.api.business_products_management.dtos.CategoryDto;
import com.api.business_products_management.models.CategoryModel;
import org.mapstruct.Mapper;
import org.springframework.stereotype.Component;
@Component
@Mapper(componentModel = "spring")
public interface CategoryMapper {
CategoryModel toCategoryModel(CategoryDto dto);
}
My pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.api</groupId>
<artifactId>business_products_management</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>business_products_management</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
<org.mapstruct.version>1.4.2.Final</org.mapstruct.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.4.2.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
As it says in the error, I'm already declaring the correct mapping in my constructor along with the interface, I can't identify the reason for this error
Upvotes: 1
Views: 3303
Reputation: 15018
As I said in the comments, you need to add the mapstruct annotation processor.
You're missing the
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
section.
Upvotes: 0