Metodo Post spring boot 2.2.2

Asked

Viewed 190 times

-1

Well guys I’m having a problem, I’m starting to learn spring boot and when I’m doing the POST method always back the error saying that it was not found

Controller

package Controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import repository.ProfissionalRepository;
import com.mv.APIRest.models.*;
@RestController
@RequestMapping(value="/api")
public class ProfissionalController {

    @Autowired
    ProfissionalRepository profissionalRespository;

    @GetMapping("/profissional")
    public List<Profissional> listaProfissional(){
        return profissionalRespository.findAll();
    }

    @GetMapping("/profissional/{id}")
    public Profissional idProfissional(@PathVariable(value="id") long id){
        return profissionalRespository.findById(id);
    }

    @PostMapping("/profissional/add")
    public Profissional addProfissional(@RequestBody Profissional profissional) {
        return profissionalRespository.save(profissional);
    }
}

Model

package com.mv.APIRest.models;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;


@Entity
@Table(name="TB_PROSSIONAL")
public class Profissional implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @NotNull
    private long id;
    @NotNull
    private String nome;
    private String endereco;
    private String telefone;
    private String telefone2;

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getEndereco() {
        return endereco;
    }
    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }
    public String getTelefone() {
        return telefone;
    }
    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }
    public String getTelefone2() {
        return telefone2;
    }
    public void setTelefone2(String telefone2) {
        this.telefone2 = telefone2;
    }

}

JSON

{
    "nome": "teste",
    "endereco": "Rua Olimpio",
    "telefone": "85998084909",
    "telefone2": "000000"
}

ERROR
{
    "timestamp": "2019-12-22T15:32:42.725+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/api/profissional/add"
}

1 answer

0

I found the error, my controller, models and Repository folders were outside the main folder.

Browser other questions tagged

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