0
I’m learning to work with Spring REST, I can already recover object by URL in Postman, but in my URL if I pass a parameter e.g.: http://localhost:8080/categories/50 It shows as status 200 OK if you found it or not, but I wanted it to show 404 otherwise it will be foundif, I came to see some examples but the example I took did not work.
*my Code
@RequestMapping("/categorias")
public class CategoriaResource {
@Autowired
private CategoriasRepository categoriasRepository;
@GetMapping("/{codigo}")
public Optional<Categoria> buscarPeloCodigo(@PathVariable Long codigo) {
return categoriasRepository.findById(codigo);
}
Exemplo que encontrei na internet; Não consigo utilizar o metodo findOne, ele pede para o retorno do meu método seja, Optional
@GetMapping("/{codigo}")
public ResponseEntity<Categoria> buscarPeloCodigo(@PathVariable Long codigo) {
Categoria categoria = categoriaRepository.findOne(codigo);
return categoria != null ? ResponseEntity.ok(categoria) : ResponseEntity.notFound().build();
}