SpringBoot - 找不到 Bean

问题描述 投票:0回答:1

运行 Spring Boot 应用程序时出现此错误:

启动 ApplicationContext 时出错。要显示条件评估报告,请在启用“调试”的情况下重新运行您的应用程序。

2024-05-11T14:59:30.328 + 04:00错误12628 --- [作业3] [主要] o.s.b.d.LoggingFailureAnalysisReporter:

****************************

应用程序无法启动

****************************

描述:

az.edu.ada.wm2.Assignment3.service.impl.BookServiceImpl 中的构造函数的参数 1 需要类型为“az.edu.ada.wm2.Assignment3.mapper.BookMapper”的 bean,但无法找到。

这是我的源文件:

Book.java

package az.edu.ada.wm2.Assignment3.model.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import jakarta.persistence.*;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String title;

    private String author;

    private String description;

    private String publishYear;
}

BookDto.java

package az.edu.ada.wm2.Assignment3.model.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class BookDto {

    private Long id;

    @NotBlank(message = "Title is required")
    @Size(max = 100, message = "Title must be less than 100 characters")
    private String title;

    @NotBlank(message = "Author is required")
    @Size(max = 50, message = "Author must be less than 50 characters")
    private String author;

    @NotBlank(message = "Description is required")
    @Size(max = 200, message = "Description must be less than 200 characters")
    private String description;
}

BookServiceImpl.java

package az.edu.ada.wm2.Assignment3.service.impl;

import az.edu.ada.wm2.Assignment3.mapper.BookMapper;
import az.edu.ada.wm2.Assignment3.model.dto.BookDto;
import az.edu.ada.wm2.Assignment3.model.entity.Book;
import az.edu.ada.wm2.Assignment3.repository.BookRepository;
import az.edu.ada.wm2.Assignment3.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BookServiceImpl implements BookService {

    private final BookRepository bookRepository;
    private final BookMapper bookMapper;

    @Autowired
    public BookServiceImpl(BookRepository bookRepository, BookMapper bookMapper){
        this.bookRepository=bookRepository;
        this.bookMapper = bookMapper;
    }


    @Override
    public List<BookDto> getAllBooks() {
        return bookRepository.findAll().stream()
                .map(bookMapper::bookToBookDto)
                .collect(Collectors.toList());
    }

    @Override
    public BookDto getBookById(Long id) {
        return bookMapper.bookToBookDto(bookRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Book not found")));
    }


    @Override
    public BookDto createBook(BookDto bookDTO) {
        Book book = bookMapper.bookDtoToBook(bookDTO);
        return bookMapper.bookToBookDto(bookRepository.save(book));
    }

    @Override
    public BookDto updateBook(Long id, BookDto bookDTO) {
        Book existingBook = bookRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Book not found"));

        existingBook.setTitle(bookDTO.getTitle());
        existingBook.setAuthor(bookDTO.getAuthor());
        existingBook.setDescription(bookDTO.getDescription());

        return bookMapper.bookToBookDto(bookRepository.save(existingBook));
    }

    @Override
    public void deleteBook(Long id) {
        bookRepository.deleteById(id);
    }
}

BookController.java

package az.edu.ada.wm2.Assignment3.controller;

import az.edu.ada.wm2.Assignment3.model.dto.BookDto;
import az.edu.ada.wm2.Assignment3.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {

    private final BookService bookService;

    @Autowired
    public BookController(BookService bookService) {
        this.bookService = bookService;
    }

    @GetMapping
    public ResponseEntity<List<BookDto>> getAllBooks() {
        List<BookDto> books = bookService.getAllBooks();
        return ResponseEntity.ok(books);
    }

    @GetMapping("/{id}")
    public ResponseEntity<BookDto> getBookById(@PathVariable Long id) {
        BookDto book = bookService.getBookById(id);
        return ResponseEntity.ok(book);
    }

    @PostMapping
    public ResponseEntity<BookDto> createBook(@Valid @RequestBody BookDto bookDTO) {
        BookDto createdBook = bookService.createBook(bookDTO);
        return ResponseEntity.status(HttpStatus.CREATED).body(createdBook);
    }

    @PutMapping("/{id}")
    public ResponseEntity<BookDto> updateBook(@PathVariable Long id, @Valid @RequestBody BookDto bookDTO) {
        BookDto updatedBook = bookService.updateBook(id, bookDTO);
        return ResponseEntity.ok(updatedBook);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBook(@PathVariable Long id) {
        bookService.deleteBook(id);
        return ResponseEntity.noContent().build();
    }
}

构建.gradle

plugins {
   id 'java'
   id 'org.springframework.boot' version '3.2.5'
   id 'io.spring.dependency-management' version '1.1.4'
}

group = 'az.edu.ada.wm2'
version = '0.0.1-SNAPSHOT'

java {
   sourceCompatibility = '17'
}

configurations {
   compileOnly {
      extendsFrom annotationProcessor
   }
}

repositories {
   mavenCentral()
}

dependencies {
   compileOnly 'org.projectlombok:lombok'
   annotationProcessor 'org.projectlombok:lombok'
   testImplementation 'org.springframework.boot:spring-boot-starter-test'
   implementation 'org.springframework.boot:spring-boot-starter-validation'

   implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
   implementation 'org.springframework.boot:spring-boot-starter-web'
   compileOnly 'org.projectlombok:lombok'
   runtimeOnly 'com.h2database:h2'
   annotationProcessor 'org.projectlombok:lombok'
   implementation 'org.mapstruct:mapstruct:1.5.5.Final'
   annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'
   testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
   useJUnitPlatform()
}

我尝试按照大多数在线解决方案中的建议使用 lombok 进行服务类,但是,这也没有帮助。您能帮我找到并解决问题吗?

java spring-boot javabeans dto mapstruct
1个回答
0
投票

错误

a bean of type 'az.edu.ada.wm2.Assignment3.mapper.BookMapper' that could not be found
意味着Spring不知道这样的bean,即Spring上下文中没有这样的bean。可能的原因有很多,但在您的情况下,它与由 mapstruct 生成的 BookMapper 有关。您没有提供 BookMapper 代码,但我想您错过了注释
@Mapper(componentModel = "spring")
,这使得 Mapper impl 类成为 Spring bean。请参阅示例:https://www.baeldung.com/mapstruct

© www.soinside.com 2019 - 2024. All rights reserved.