1
resposta

Error: Not Found - Postman

Boa tarde!

Estou com o seguinte problema na hora de fazer o GET no Postman, não consegui localizar o erro...

{
    "timestamp": "2022-09-12T15:48:35.199+00:00",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/seguro/clientes/lista"
}

Controller:

package br.com.seguro.controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import br.com.seguro.documents.Cliente;
import br.com.seguro.repository.ClienteRepository;
import br.com.seguro.responses.Response;

@RestController
@RequestMapping("/seguro/clientes")
public class ClienteController {

    @Autowired
    private ClienteRepository clienteService;

    Cliente clt = new Cliente();

    @GetMapping ("/lista")
    public ResponseEntity<List<Cliente>> listarClientes(){
        try {
            List<Cliente> clt2 = new ArrayList<Cliente>();

            clienteService.findAll().forEach(clt2::add);

            if(clt2.isEmpty()) {
                return new ResponseEntity<>(HttpStatus.NO_CONTENT);
            }

            return new ResponseEntity<>(clt2, HttpStatus.OK);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    @GetMapping("/{id}")
    public ResponseEntity<Cliente> ListarPorId(@PathVariable("id") String id){
        Optional<Cliente> cliente = clienteService.findById(id);

        if (cliente.isPresent()) {
            return new ResponseEntity<>(cliente.get(), HttpStatus.OK);
        }else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    @PostMapping(value = "/novo")
    public ResponseEntity<Cliente> cadastrar(@RequestBody Cliente request){
        return ResponseEntity.ok(clienteService.save(request));
    }

    @PutMapping(path = "/{id}")
    public ResponseEntity<Response<Cliente>> atualizar(@PathVariable(name = "id") String id,@Valid @RequestBody Cliente cliente, BindingResult result) {
        if (result.hasErrors()) {
            List<String> erros = new ArrayList<String>();
            result.getAllErrors().forEach(erro -> erros.add(erro.getDefaultMessage()));
            return ResponseEntity.badRequest().body(new Response<Cliente>(erros));
        }        
        cliente.setId(id);
        return ResponseEntity.ok(new Response<Cliente>(this.clienteService.save(cliente)));
    }

    @DeleteMapping(path = "/{id}")
    public ResponseEntity<Response<Integer>> remover(@PathVariable(name = "id") String id) {    
        this.clienteService.deleteById(id);
        return ResponseEntity.ok(new Response<Integer>(1));
    }
}

SeguroApplication:

package br.com.seguro.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
@Configuration
public class SeguroApplication {


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

    }

}
1 resposta

Olá Gabriel,

Pelo que pude entender, você está recebendo um erro "Not Found" ao tentar fazer uma requisição GET no Postman para o endpoint "/seguro/clientes/lista". Esse erro pode ocorrer quando o servidor não encontra o recurso solicitado.

Verifique se o endpoint está correto e se o servidor está em execução. Além disso, verifique se o método HTTP está correto. No seu código, você definiu o método GET para o endpoint "/seguro/clientes/lista", então certifique-se de que está usando o mesmo método na requisição no Postman.

Outra coisa que pode ajudar é verificar se o seu servidor está retornando a resposta correta para essa requisição. Você pode testar isso usando o próprio navegador ou alguma ferramenta de teste de API, como o Swagger.

Espero ter ajudado e bons estudos!