简体   繁体   中英

How to send a Java Long value from json

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM