Estou começando aprender Spring e estou recebendo esse erro, já fiz diversas pesquisas na internet porém até o momento não entendi o que está causando o problema:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookController' defined in file [C:\bookstroremanager\bookStoreManager\target\classes\com\denilsonf\bookstoremaneger\controller\BookController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookService' defined in file [C:\bookstroremanager\bookStoreManager\target\classes\com\denilsonf\bookstoremaneger\service\BookService.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.denilsonf.bookstoremaneger.service.BookService]: Constructor threw exception; nested exception is java.lang.ExceptionInInitializerError
Classe BookController :
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.denilsonf.bookstoremaneger.dto.BookDTO;
import com.denilsonf.bookstoremaneger.dto.MessageResponseDTO;
import com.denilsonf.bookstoremaneger.service.BookService;
@RestController
@RequestMapping("/api/v1/books")
public class BookController {
private BookService bookService;
@Autowired
public BookController(BookService bookService){
this.bookService = bookService;
}
@PostMapping
public MessageResponseDTO create(@RequestBody @Valid BookDTO bookDTO){
return bookService.create(bookDTO);
}
}
Classe BookService:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.denilsonf.bookstoremaneger.dto.BookDTO;
import com.denilsonf.bookstoremaneger.dto.MessageResponseDTO;
import com.denilsonf.bookstoremaneger.entity.Book;
import com.denilsonf.bookstoremaneger.mapper.BookMapper;
import com.denilsonf.bookstoremaneger.repository.BookRepository;
@Service
public class BookService {
private BookRepository bookRepository;
private final BookMapper bookMapper = BookMapper.INSTANCE;
@Autowired
public BookService(BookRepository bookRepository){
this.bookRepository = bookRepository;
}
public MessageResponseDTO create(BookDTO bookDTO){
Book bookToSave = bookMapper.toModel(bookDTO);
Book savedBook = bookRepository.save(bookToSave);
return MessageResponseDTO.builder()
.message("Book created with ID " + savedBook.getId())
.build();
}
}
Classe BookMapper:
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import com.denilsonf.bookstoremaneger.dto.BookDTO;
import com.denilsonf.bookstoremaneger.entity.Book;
@Mapper
public interface BookMapper {
BookMapper INSTANCE = Mappers.getMapper(BookMapper.class);
Book toModel(BookDTO bookDTO);
BookDTO toDTO(Book book);
}