spring API Java Sping Boot 时出错-错误16764

c3frrgcw  于 12个月前  发布在  Spring
关注(0)|答案(1)|浏览(133)

你没事吧?我试图创建一个简单的文档注册API,启动时出现以下错误。这是一个小API,所以我将把所有的类
主计长:Beneficíario

package pldesaude.CadastroDeBeneficiario.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pldesaude.CadastroDeBeneficiario.entity.Beneficiario;
import pldesaude.CadastroDeBeneficiario.repository.BeneficiarioRepository;

import java.util.List;

@RestController
@RequestMapping("api/beneficiarios")
public class BeneficiarioController {
    @Autowired
    private BeneficiarioRepository beneficiarioRepository;

    @GetMapping
    public List<Beneficiario> listarBeneficiarios(){
        return beneficiarioRepository.findAll();
    }
    @PostMapping
    public Beneficiario criarBeneficiario(@RequestBody Beneficiario beneficiario){
        return beneficiarioRepository.save(beneficiario);
    }

}

字符串
控制器文档

package pldesaude.CadastroDeBeneficiario.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pldesaude.CadastroDeBeneficiario.entity.Documento;
import pldesaude.CadastroDeBeneficiario.repository.DocumentoRepository;

import java.util.List;

@RestController
@RequestMapping("api/documentos")
public class DocumentoController {

    @Autowired
    private DocumentoRepository documentoRepository;

    @GetMapping
    public List<Documento> listaDocumentos() {
        return documentoRepository.findAll();
    }

    @PostMapping
    public Documento criarDocumento(@RequestBody Documento documento){
        return documentoRepository.save(documento);
    }
}


实体:受益人:

package pldesaude.CadastroDeBeneficiario.entity;

import javax.persistence.*;
import java.util.Date;

@Entity
public class Beneficiario {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String nome;
    @Temporal(TemporalType.DATE)
    private Date dataNascimento;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public Date getDataNascimento() {
        return dataNascimento;
    }

    public void setDataNascimento(Date dataNascimento) {
        this.dataNascimento = dataNascimento;
    }
}


实体文件:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Date;

@Entity
public class Documento {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String tipoDocumento;
    private String descricao;
    private Date dataAtualizacao;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTipoDocumento() {
        return tipoDocumento;
    }

    public void setTipoDocumento(String tipoDocumento) {
        this.tipoDocumento = tipoDocumento;
    }

    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    public Date getDataAtualizacao() {
        return dataAtualizacao;
    }

    public void setDataAtualizacao(Date dataAtualizacao) {
        this.dataAtualizacao = dataAtualizacao;
    }
}


接口:受益人:

package pldesaude.CadastroDeBeneficiario.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import pldesaude.CadastroDeBeneficiario.entity.Beneficiario;

@Repository
public interface BeneficiarioRepository extends JpaRepository<Beneficiario, Long> {
}


接口:Documento

package pldesaude.CadastroDeBeneficiario.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import pldesaude.CadastroDeBeneficiario.entity.Documento;

@Repository
public interface DocumentoRepository extends JpaRepository<Documento, Long> {
}


适用范围:

package pldesaude.CadastroDeBeneficiario;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CadastroDeBeneficiarioApplication {

    public static void main(String[] args) {
        SpringApplication.run(CadastroDeBeneficiarioApplication.class, args);
    }

}


性能

spring.datasource.url=jdbc:h2:mem:mydb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=sa
spring.jpa.hibernate.ddl-auto=update
spring.h2.console.enabled=true


错误代码:

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-11-08T17:12:29.823-03:00 ERROR 9860 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Field beneficiarioRepository in pldesaude.CadastroDeBeneficiario.controller.BeneficiarioController required a bean of type 'pldesaude.CadastroDeBeneficiario.repository.BeneficiarioRepository' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'pldesaude.CadastroDeBeneficiario.repository.BeneficiarioRepository' in your configuration.

Process finished with exit code 0


Arquivo pom.xml

<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.1.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>pldesaude.aplicacao</groupId>
    <artifactId>CadastroDeBeneficiario</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>CadastroDeBeneficiario</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.3.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>3.1.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

ev7lccsx

ev7lccsx1#

尝试将此依赖项添加到pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

字符串

相关问题