How to make the findById method in spring-boot?

Asked

Viewed 192 times

-3

I’m making an Api where I need to get a user’s information through findById, but the STATUS in Postman is like 200 and did not display anything. Code below.

controller

@RestController
@RequestMapping("/bank")
public class ClienteController {
    
    @Autowired
    ClienteService clienteService;
    
    //Listar todos os cliente
    @GetMapping("/cliente")
    public List<Cliente> findAll() { 
        return clienteService.findAll();
    }
    
    //Procurar por apenas 1 cliente
    // não esta funcionando. http 200 mas não aparece informação.
    @GetMapping("/cliente/{id}")
    public void clienteFindById(@PathVariable("id")Long id, @RequestBody Cliente clientes ){
        clienteService.clienteFindById(clientes, id);
    }
}

Service

@Service
public class ClienteService {
    
    @Autowired
    ClienteRepository clienteRepository;
    
    public List<Cliente> findAll(){
        return clienteRepository.findAll() ;
    }
    
    
    
    public Optional<Cliente> clienteFindById(Cliente clientes, Long id) {
        return clienteRepository.findById(id);
    }
}
  • Add the source code as text to the question. Learn more Welcome to [en.so]! You have placed a code image and/or error message. Although it sounds like a good idea, it’s not! One of the reasons is that if someone wants to answer the question, they can’t copy the code and change something inside. Click on the [Edit] link and put the code/error as text. See more about this in these links - Manual on how NOT to ask questions, Post Error Message as Picture.

2 answers

1


João, the method in the controller is returning void, when it should return an Optional Client.

Correção

That adjustment should solve your problem.

0

Beyond the Optional you can also use a ReponseEntinty. Including, when using the Optional take care and check if it is returning any value.

Browser other questions tagged

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