简体   繁体   English

如何从 json 发送 Java Long 值

[英]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.我正在尝试向带有请求正文的 Spring RestController 发送 POST 请求。 In the object there is a Long value but it is not arriving to the endpoint with the other parameters.在对象中有一个 Long 值,但它没有与其他参数一起到达端点。 The class used as @RequestBody is this one:用作@RequestBody的类是这样的:

@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:@RestController注释的类中的端点是这样的:

    @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:这是我在 POST 请求正文中使用的 JSON:

{
    "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.所有其他参数都正确到达并且对象到达数据库,但profesorid值。 I stopped the execution y this addCurso method and the value of profesorid is null.我停止了这个addCurso方法的执行, profesorid 的值为空。 The id value is not being sent in the request because it is setted before saving in the database. id值没有在请求中发送,因为它是在保存到数据库之前设置的。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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