No Qualifying bean of type found for service

Asked

Viewed 742 times

0

Good evening guys, I’m trying to launch a Spring API and I’m having the following problem running it, I’ve tried renaming the methods as well as the functions

First I’ll leave the Debug log here

Field service in com.zaleski.apieadteste.apieadteste.controller.ControladorCurso required a bean of type 'com.zaleski.apieadteste.apieadteste.service.ServicoCurso' that could not be found.

Action: Consider Defining a bean of type 'com.zaleski.apieadteste.apieadteste.service.Servicocurso' in your Configuration.

Before that when running the application appears this error here, if it also helps:

2020-08-12 21:04:26.806 WARN 13260 --- [ restartedMain] Configservletwebserverapplicationcontext : Exception encountered During context initialization - cancelling refresh Attempt: org.springframework.Beans.factory.Unsatisfieddependencyexception: Error Creating bean with name 'controllerCurso': Unsatisfied dependency Expressed through field 'service'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zaleski.apieadteste.apieadteste.service.ServicoCurso' available: expected at least 1 bean which qualifies as autowire candidate. Dependency Annotations: {@org.springframework.Beans.factory.Annotation.Autowired(required=true)}

Follow my Class Course:

package com.zaleski.apieadteste.apieadteste.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;


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


@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "CURSOS_TBL")

public class Curso {
    @Id
    @GeneratedValue
    private int id;
    private String nome_curso;
    private String duracao;
    private String area_atuacao;
    private String portaria_mec;
    private String ementa;
    private double valor_mensalidade;
    private int qtde_vagas;
}

Follow my Servicocurso Class.java

package com.zaleski.apieadteste.apieadteste.service;

import java.util.List;
import com.zaleski.apieadteste.apieadteste.entity.Curso;
import com.zaleski.apieadteste.apieadteste.repository.RepositorioCurso;

import org.springframework.beans.factory.annotation.Autowired;

public class ServicoCurso {
    @Autowired
    private RepositorioCurso repository;
    
    public Curso saveCurso(Curso curso){
        return repository.save(curso);
    }
    public List<Curso> saveCursos(List<Curso> cursos){
        return repository.saveAll(cursos);
    }

    public List<Curso> getCursos(){
        return repository.findAll();
    }

    public Curso getCursoById(int id){
        return repository.findById(id).orElse(null);
    }

    public Curso getCursoByName(String name){
        return repository.findByName(name);
    }

    public String deleteCurso(int id){
        repository.deleteById(id);
        return "curso removido = "+id;
    }

    public Curso updateCurso(Curso curso){
        Curso existingCurso = repository.findById(curso.getId()).orElse(null);
        existingCurso.setNome_curso(curso.getNome_curso());
        existingCurso.setDuracao(curso.getDuracao());
        existingCurso.setArea_atuacao(curso.getArea_atuacao());
        existingCurso.setEmenta(curso.getEmenta());
        existingCurso.setPortaria_mec(curso.getPortaria_mec());
        existingCurso.setValor_mensalidade(curso.getValor_mensalidade());
        existingCurso.setQtde_vagas(curso.getQtde_vagas());

        return repository.save(existingCurso);
    }
    
}

Follow My Controller Course.java

package com.zaleski.apieadteste.apieadteste.controller;

import java.util.List;

import com.zaleski.apieadteste.apieadteste.entity.Curso;
import com.zaleski.apieadteste.apieadteste.service.ServicoCurso;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController

public class ControladorCurso {

    @Autowired
    private ServicoCurso service;

    @PostMapping("/addCurso")
    private Curso addCurso(@RequestBody Curso curso){
        return service.saveCurso(curso);
    }

    @PostMapping("/addCursos")
    private List<Curso> addCursos(@RequestBody List<Curso> cursos){
        return service.saveCursos(cursos);
    }

    @GetMapping("/buscaCursos")
    public List<Curso> findAllCursos() {
        return service.getCursos();
    }

    @GetMapping("/buscaCursosById/{id}")
    public Curso findCursosById(@PathVariable int id){
        return service.getCursoById(id);
    }

    @GetMapping("/buscaCursos/{name}")
    public Curso findcursoById(@PathVariable String nome){
        return service.getCursoByName(nome);
    }

    @PutMapping("/updateCurso")
    public Curso updateCurso(@RequestBody Curso curso){
        return service.updateCurso(curso);
    }

    @DeleteMapping("/deleteCurso/{id}")
    public String deleteCurso(@RequestBody int id){
        return service.deleteCurso(id);
    }
    
}

and Follow my Repositoriocurso.java

package com.zaleski.apieadteste.apieadteste.repository;

import com.zaleski.apieadteste.apieadteste.entity.Curso;
import org.springframework.data.jpa.repository.JpaRepository;

public interface RepositorioCurso extends JpaRepository<Curso,Integer> {
    Curso findByName(String nome_curso);
}

Any help is welcome, grateful

1 answer

0

You forgot the note @Service in your class ServicoCurso. The annotation will make it detectable by Spring, and therefore be available for injection into your controller:

@Service
public class ServicoCurso {
    ...
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.