When I make a request on my Rest API with Spring boot, it is coming with an extra item

Asked

Viewed 447 times

1

I have debugged and the result is coming out a correct list, but when I request in Postman, comes an extra item. Looks like a counter. Can someone tell me what this is and how to take?

inserir a descrição da imagem aqui

This is my controller.

import javax.servlet.http.HttpServletResponse;

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.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import com.uezohub.backend.domain.models.Curso;
import com.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 model

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;
    }


}

1 answer

1


Your model has a method getCurso() which returns the id. So when serializing to JSON it is printing the key curso of the same value as id. To adjust just remove the method getCurso.

  • Po was just that, thank you very much !! I hadn’t noticed because I looked at the attributes and didn’t look at the methods. The framework understands these methods as properties right ? I hadn’t noticed this, thank you !!

  • This, as in other frameworks are used methods get of its class to read an object. Any method get can be understood as a property of your class by the framework.

  • Got it, thank you very much !!

Browser other questions tagged

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