JSON 解析错误:无法反序列化 'java.lang.Long 的实例



我正在使用弹簧引导,我尝试创建一个新学生。

@RequestMapping(path="/student/create", method=RequestMethod.POST)<br>
public ResponseEntity<String> createStudent(@RequestBody Long nummer, String firstname, String lastname){<br>
service.createStudent(matrikelnummer, vorname, nachname);<br>
return new ResponseEntity<String>("gut gemacht", HttpStatus.OK);

这是我与 RESTCLIENTRequestBody

{"nummer":15557,"firstname":"toti","lastname":"Innna"}

我有这个错误

{"时间戳":">

2019-12-20T19:41:30.083+0000","状态":400,"错误":"不好 请求","消息":"JSON 解析错误:无法反序列化 的实例java.lang.Long出START_OBJECT令牌;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException: 不能 从令牌中反序列化java.lang.LongSTART_OBJECT实例 在 [来源: (PushbackInputStream(; 行: 1, 列: 1]","trace":"org.springframework.http.converter.HttpMessageNotReadableException: JSON 解析错误:无法反序列化java.lang.Long的实例 START_OBJECT令牌;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException: 不能 从令牌中反序列化java.lang.LongSTART_OBJECT实例 at [Source: (PushbackInputStream(; 行: 1, 列: 1]\r\tat

在您的示例中,映射器希望主体呈现一个 Long 对象,但您向它传递了一个 Student 对象。这不匹配,因此会引发异常。

没有必要将学生的所有字段列为单独的方法参数,您只需将 Student 对象作为 RequestBody 参数传递即可。 然后,对象映射器将尝试从提供的 JSON 解析学生实例。

例:

@RequestMapping(path="/student/create", method=RequestMethod.POST)
public ResponseEntity<String> createStudent(@RequestBody Student student){
service.createStudent(student);
return new ResponseEntity<String>("gut gemacht", HttpStatus.OK);
}

相关内容

最新更新