Java Springboot中的json解析错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44582670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
json parsing error in Springboot
提问by Roopesh Majeti
I have a simple springboot program which takes a json and prints it. The main intention was to do json validator package usage, but the current context is on the basic request parsing. The problem is when i tryy to map the input request into an class entity, it is giving the below error : "org.springframework.http.converter.HttpMessageNotReadableException",.
我有一个简单的 springboot 程序,它接受一个 json 并打印它。主要目的是做 json 验证器包的使用,但当前的上下文是在基本的请求解析上。问题是当我尝试将输入请求映射到类实体时,它给出了以下错误:“org.springframework.http.converter.HttpMessageNotReadableException”,。
Controller ( Hello.java ) :
@RequestMapping(method = RequestMethod.POST , consumes = "application/json") public ResponseEntity<String> welcome( @RequestBody DemoEntity demoEntity ) { System.out.println(demoEntity.getName()); String response ="success"; return new ResponseEntity<>(response, HttpStatus.CREATED); } }
Java Class entity :
public class DemoEntity implements Serializable {
@JsonProperty("name") private String name; @JsonProperty("no") private int no; public int getNo() { return no; } public void setNo(int no) { this.no = no; } public String getName() { return name; } public void setName(String name) { this.name = name; } DemoEntity(String name) { this.name = name; } }
- Complete Exception :
{ "timestamp": 1497594485418, "status": 400, "error": "Bad Request", "exception": "org.springframework.http.converter.HttpMessageNotReadableException", "message": "JSON parse error: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value; nested exception is com.fasterxml.Hymanson.core.JsonParseException: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value\n at [Source: java.io.PushbackInputStream@75be93a7; line: 1, column: 3]", "path": "/welcome" }
控制器(Hello.java):
@RequestMapping(method = RequestMethod.POST , consumes = "application/json") public ResponseEntity<String> welcome( @RequestBody DemoEntity demoEntity ) { System.out.println(demoEntity.getName()); String response ="success"; return new ResponseEntity<>(response, HttpStatus.CREATED); } }
Java 类实体:
公共类 DemoEntity 实现了 Serializable {
@JsonProperty("name") private String name; @JsonProperty("no") private int no; public int getNo() { return no; } public void setNo(int no) { this.no = no; } public String getName() { return name; } public void setName(String name) { this.name = name; } DemoEntity(String name) { this.name = name; } }
- 完全异常:
{“时间戳”:1497594485418,“状态”:400,“错误”:“错误请求”,“异常”:“org.springframework.http.converter.HttpMessageNotReadableException”,“消息”:“JSON解析错误:意外字符( '-'(代码 45))在数值中:预期数字 (0-9) 跟随减号,对于有效数值;嵌套异常是 com.fasterxml.Hymanson.core.JsonParseException:意外字符('-'(代码) 45)) 数值:预期数字 (0-9) 跟随减号,对于有效数值\n 在 [来源:java.io.PushbackInputStream@75be93a7; 行:1,列:3]","路径" : “/欢迎” }
Sample inputrequest in the body : {"name":"Roopesh", "no":123123}
正文中的示例输入请求:{"name":"Roopesh", "no":123123}
采纳答案by invenit
You send incorrect request. Use curl -X POST localhost:8090/one -H 'content-type: application/json;charset=UTF-8' -H 'name: test' -H 'postman-token: 8e87369d-e2e2-ab25-eadd-f40f0682e593' -d '{"name":"Roopesh", "no":"123123"}'
您发送了错误的请求。用curl -X POST localhost:8090/one -H 'content-type: application/json;charset=UTF-8' -H 'name: test' -H 'postman-token: 8e87369d-e2e2-ab25-eadd-f40f0682e593' -d '{"name":"Roopesh", "no":"123123"}'
- Don't sent
demoEntity=
. Body should contains just json itself. - Use
-d
key to send data.-F
is for multipart body. It is a little bit different.
- 不送
demoEntity=
。Body 应该只包含 json 本身。 - 使用
-d
密钥发送数据。-F
用于多部分身体。它有点不同。
回答by Kundan Atre
Incase if you are using Postman client to test your Rest API, there are chances wherein you must be adding body in under tab "form-data"rather "raw".
如果您使用 Postman 客户端来测试您的 Rest API,则有可能您必须在选项卡"form-data"而不是"raw"下添加正文。