Springboot中的json解析错误 [英] json parsing error in Springboot

查看:173
本文介绍了Springboot中的json解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 springboot 程序,它接受一个 json 并打印它.主要目的是做 json 验证器包的使用,但当前的上下文是在基本的请求解析上.问题是当我尝试将输入请求映射到类实体时,它给出了以下错误:org.springframework.http.converter.HttpMessageNotReadableException",.

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",.

  • 控制器(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 {

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

    • 完全异常:
    • {时间戳":1497594485418,状态":400,"error": "错误的请求","异常": "org.springframework.http.converter.HttpMessageNotReadableException",消息":JSON 解析错误:数值中出现意外字符('-'(代码 45)):预期数字 (0-9) 跟随减号,对于有效数值;嵌套异常是 com.fasterxml.jackson.core.JsonParseException: 数值中的意外字符 ('-' (code 45)):预期数字 (0-9) 跟随减号,对于有效数值\n 在 [Source: java.io.PushbackInputStream@75be93a7; line: 1, 列: 3]",路径":/欢迎"}

      { "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.jackson.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" }

      示例输入请求正文:{"name":"Roopesh", "no":123123}

      Sample input request in the body : {"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"}'

      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"}'

      1. 不要发送demoEntity=.正文应仅包含 json 本身.
      2. 使用 -d 键发送数据.-F 用于多部分主体.有点不同.
      1. Don't sent demoEntity=. Body should contains just json itself.
      2. Use -d key to send data. -F is for multipart body. It is a little bit different.

      这篇关于Springboot中的json解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆