Problem with Bean Springboot

Asked

Viewed 20 times

0

Good morning, people I am doing an API with spring boot but my bean is not being located, someone could help me, the other time I did not find this problem. Follows Codes:

 @RestController
    @RequestMapping("/api")
    public class PessoaController {

        private PessoaImpl pesimpl;


        @Autowired
        public PessoaController(PessoaImpl pesimpl) {
            super();
            this.pesimpl = pesimpl;
        }

        @GetMapping(value="/buscar", produces="application/json")
        public String trazerTodos() {
            return "Tudo aqui de novo!";
        }

        @PostMapping("/enviarDados")
        public void inserirDados(@RequestBody String data) {

        }

    }

@Service
public class PessoaImpl implements PessoaService {

    private PessoaRepo pessoaRepository;

    @Autowired
    public PessoaImpl(PessoaRepo pessoaRepository) {
        super();
        this.pessoaRepository = pessoaRepository;
    }

    @Override
    public List<Pessoa> buscarTodos() {
        return this.pessoaRepository.findAll();
    }

}


@Document(collection = "pessoa")
public class Pessoa {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private String id;

    private String nome;
    private int contato;


    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public int getContato() {
        return contato;
    }
    public void setContato(int contato) {
        this.contato = contato;
    }



}


public interface PessoaRepo extends MongoRepository<Pessoa, String>{

    List<Pessoa> findAll();

}


public interface PessoaService {

    public List<Pessoa> buscarTodos();

}


@ComponentScan({"com.example.controllers"})
@EnableAutoConfiguration
@SpringBootApplication
public class TestandoApplication {

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

}

Would it be something related to Annotation? If you can help me

  • What do you mean by "my bean is not being located", who did not? What error are you getting? at what point? you can change your question?

1 answer

0

I believe that the correct thing would be to use the interface and not the class that implements it:

    @RestController
    @RequestMapping("/api")
    public class PessoaController {

        @Autowired
        private PessoaService pesservice;

        @GetMapping(value="/buscar", produces="application/json")
        public String trazerTodos() {
            return "Tudo aqui de novo!";
        }

        @PostMapping("/enviarDados")
        public void inserirDados(@RequestBody String data) {

        }

    }

Thus it is not necessary to use a constructor either

Browser other questions tagged

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