2
respostas

Erro 404

Ao disparar o Postman estou tomando este erro! { "timestamp": "2022-01-20T11:49:44.325+0000", "status": 404, "error": "Not Found", "message": "No message available", "path": "/topicos/3"

2 respostas

Oi Carlos,

Posta aqui o código da sua classe controller

Boa noite!

package br.com.alura.forum.controller;

import java.net.URI; import java.util.Arrays; import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; 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.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.util.UriBuilder; import org.springframework.web.util.UriComponentsBuilder;

import br.com.alura.forum.controller.dto.DetalhesDoTopicoDto; import br.com.alura.forum.controller.dto.TopicoDto; import br.com.alura.forum.controller.form.TopicoForm; import br.com.alura.forum.controller.repository.CursoRepository; import br.com.alura.forum.modelo.Curso; import br.com.alura.forum.modelo.Topico; import br.com.alura.forum.repository.TopicoRepository;

@RestController @RequestMapping("/topicos") public class TopicosController {

@Autowired
private CursoRepository cursoRepository;

@Autowired
private TopicoRepository topicoRepository;

@GetMapping
public List<TopicoDto> lista(String nomeCurso) {


    if (nomeCurso == null) {
        List<Topico> topicos = topicoRepository.findAll();
        return TopicoDto.converter(topicos);
 } else {
     List<Topico> topicos = topicoRepository.findByCursoNome(nomeCurso);
         return TopicoDto.converter(topicos);
 }

} @PostMapping public ResponseEntity cadastrar(@RequestBody @Valid TopicoForm form, UriComponentsBuilder uriBuilder) { Topico topico = form.converter(cursoRepository); topicoRepository.save(topico);

    URI uri = uriBuilder.path("/topicos/{id}").buildAndExpand(topico.getId()).toUri() ;
    return ResponseEntity.created(uri).body(new TopicoDto(topico));
}

  @GetMapping("/{id}")
  public TopicoDto detalhar(@PathVariable Long id) {
      Topico topico = topicoRepository.getOne(id);
      return new TopicoDto(topico);

  }

}