When I make a request in my API Rest with Spring boot, it is coming with one more item

1

I have already debugged and the result is a correct list, but when I make the request in the Postman, there is one more item. Looks like an accountant. Can someone tell me what this is and how to pull it off?

Thisismycontroller.

importjavax.servlet.http.HttpServletResponse;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.http.ResponseEntity;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.PostMapping;importorg.springframework.web.bind.annotation.RequestBody;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importorg.springframework.web.servlet.support.ServletUriComponentsBuilder;importcom.uezohub.backend.domain.models.Curso;importcom.uezohub.backend.domain.service.CursoService;@RestController@RequestMapping("/curso")
public class CursoResource {

    @Autowired
    private CursoService cursoService;

    @GetMapping
    public List<Curso> buscarCursos() {
        return cursoService.buscarTodos();
    }

    @PostMapping
    public ResponseEntity<Curso> salvarCurso(@RequestBody Curso curso, HttpServletResponse response) {
        Curso cursoSalvo = cursoService.salvar(curso);
        URI uri = ServletUriComponentsBuilder.fromCurrentRequestUri()
                    .path("/{id}").buildAndExpand(cursoSalvo.getId()).toUri();
        response.setHeader("Location", uri.toASCIIString());


        return ResponseEntity.created(uri).body(cursoSalvo);
    }

    @GetMapping("/{id}")
    public Curso buscarCursoPorId(@PathVariable Long id) {
        return cursoService.buscarPorId(id);
    }
}

This is my template

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "curso")
public class Curso {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String nome;
    private boolean ativo;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public Long getCurso() {
        return id;
    }
    public void setCurso(Long curso) {
        this.id = curso;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public boolean isAtivo() {
        return ativo;
    }
    public void setAtivo(boolean ativo) {
        this.ativo = ativo;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (ativo ? 1231 : 1237);
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((nome == null) ? 0 : nome.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Curso other = (Curso) obj;
        if (ativo != other.ativo)
            return false;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (nome == null) {
            if (other.nome != null)
                return false;
        } else if (!nome.equals(other.nome))
            return false;
        return true;
    }


}
    
asked by anonymous 30.11.2017 / 02:34

1 answer

1

Your model has a getCurso() method that returns the id. So at the time of serializing to JSON it is printing the key curso with the same value as id . To adjust, simply remove the getCurso method.

    
30.11.2017 / 02:41