Reputation: 52
I'm trying to send a POST request to an Spring RestController with a request body. In the object there is a Long value but it is not arriving to the endpoint with the other parameters.
The class used as @RequestBody
is this one:
@Entity
public class Curso {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "titulo")
private String titulo;
@Column(name = "nivel")
private String nivel;
@Column(name = "nhoras")
private String nhoras;
@Column(name = "profesorid")
private Long profesorid;
[...]
public Long getProfesorid() {
return profesorid;
}
public void setProfesorid(Long profesor) {
this.profesorid = profesorid;
}
}
The endpoint in the class annotated with @RestController
, is this:
@RequestMapping(value = "/crear-curso", method = RequestMethod.POST)
public void addCurso(@RequestBody Curso curso) {
cursoService.addCurso(curso);
}
And this is the JSON I'm using in the body of the POST request:
{
"titulo": "Git",
"nivel": "Intermedio",
"nhoras": "12",
"profesorid": 1581068174
}
All the other parameters are arriving correctly and the object arrives to the database, but with the profesorid
with null value. I stopped the execution y this addCurso method and the value of profesorid is null. The id value is not being sent in the request because it is setted before saving in the database.
Please, anyone can help me and say what is failing here? Many thanks in advance.
Upvotes: 0
Views: 7704
Reputation: 1524
public void setProfesorid(Long profesor) {
this.profesorid = profesorid;
}
Look at this setter. You've made a mistake here.
It should be
public void setProfesorid(Long profesor) {
this.profesorid = profesor;
}
Upvotes: 6