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?
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;
}
}
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 !!
– Igor Ramos
This, as in other frameworks are used methods
get
of its class to read an object. Any methodget
can be understood as a property of your class by the framework.– Andre Gusmao
Got it, thank you very much !!
– Igor Ramos