0

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

  • 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.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" }

Sample input request in the body : {"name":"Roopesh", "no":123123}

5
  • The "sample input" is unlikely to lead to this error message since it does not contain a single - sign. Please show the exact input leading to this error. Commented Jun 16, 2017 at 6:57
  • Your example input doesn't correspond to Exception. Exception says that there is an unexpected character '-' but I don't see it in sample input. Commented Jun 16, 2017 at 6:57
  • 1
    I tried your code and it seems to work on my machine. You only have to add empty constructor to DemoEntity. Commented Jun 16, 2017 at 7:13
  • Here is the request sample : curl -X POST \ localhost:8080/welcome \ -H 'content-type: application/json;charset=UTF-8' \ -H 'name: test' \ -H 'postman-token: 8e87369d-e2e2-ab25-eadd-f40f0682e593' \ -F 'demoEntity={"name":"Roopesh", "no":"123123"}' Commented Jun 16, 2017 at 7:48
  • Tried using curl, POSTman both, but same error. Commented Jun 16, 2017 at 7:50

2 Answers 2

5

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. 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.
Sign up to request clarification or add additional context in comments.

Comments

5

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

1 Comment

yes, I face the same problem by sending in raw helped me, the property name must be in double quotes and if a string value is there then double quote for numeric value works without a double quote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.