Reputation: 554
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;
}
}
{ "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}
Upvotes: 0
Views: 47707
Reputation: 3961
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".
Upvotes: 5
Reputation: 424
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"}'
demoEntity=
. Body should contains just json itself.-d
key to send data. -F
is for multipart body. It is a little bit different.Upvotes: 5