0

I have been unable to receive information from a JSON send through a POST request, that looks something like this:

[{
 "idVehicule": 1,
 "vacancies": 3
}]

I have a simple controller that tries to get that JSON sent from front, convert it into a testModel:

import com.fasterxml.jackson.annotation.JsonProperty;

public class testModel {

    @JsonProperty( "idvehicle" )
    private int idvehicle;
    @JsonProperty( "vacancies" )
    private String vacancies;

    public int getIdvehicle() {
        return idvehicle;
    }
    public void setIdvehicle(int idvehicle) {
        this.idvehicle = idvehicle;
    }
    public String getVacancies() {
        return vacancies;
    }
    public void setVacancies(String vacancies) {
        this.vacancies = vacancies;
    }

}

And then it just prints one of its values.

@RequestMapping(value = "/vehicle", method = RequestMethod.POST)
    public ResponseEntity<String> vehicleTest(@RequestBody testModel testModel){
        System.out.println(testModel.getVacancies());
        return new ResponseEntity<String>(HttpStatus.OK);;
    }

After trying the method with postman, i keep getting this error:

{
  "timestamp": 1472819769941,
  "status": 400,
  "error": "Bad Request",
  "exception": "org.springframework.http.converter.HttpMessageNotReadableException",
  "message": "Could not read document: Can not deserialize instance of testModel out of START_ARRAY token\n at [Source: java.io.PushbackInputStream@646345e6; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of testModel out of START_ARRAY token\n at [Source: java.io.PushbackInputStream@646345e6; line: 1, column: 1]",
  "path": "/vehicle"
}

I have also tried changing the JSON and then the problem is that the method is unable to convert it into the enity, making the variable "testModel" always null

{"testModel":{"idvehicle":1,"vacancies":3}}

Removing the "@RequestBoby" annotation gives the same problem that the paragraph before.

Any ideas that could help me figure out the problem? Thanks

2
  • 2
    Send it as plain JSON... {"idvehicle":1,"vacancies":3}. Also idvehicle will not match setIdVehicle it should be idVehicle in that case. Commented Sep 2, 2016 at 13:04
  • Please place this as an answer so i can accept it, since this is the correct one. Thanks Commented Sep 5, 2016 at 7:00

3 Answers 3

1

You are specifying @JsonProperty as idvehicle in your testModel so correct the JSON you are posting to:

 [{
     "idvehicle": 1,
     "vacancies": 3
 }]

Next, you are sending the array of testModel and expecting testModel which will of-course will not deserialize.

Either correct the JSON you are sending to {"idvehicle": 1,"vacancies": 3} or change the Controller to accept array of testModel as follows:

@RequestMapping(value = "/vehicle", method = RequestMethod.POST)
public ResponseEntity<String> vehicleTest(@RequestBody List<testModel> testModel){

        return new ResponseEntity<String>(HttpStatus.OK);;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, well explained, i decided to change the JSON structure as advice for you and @M. Deinum , also the problem was that the JSON fields must be identical to the class attributes.
1

JsonProperty is case sensitive. You need to have the key name exactly same as that in Json. So change it like this and check the spelling also.

@JsonProperty( "idVehicule" )

Comments

0

You're POSTing an array of of testModel objects (quick gripe, classnames should start with uppercase letters, so it should be TestModel) but your method accepts one testModel as @RequestBody. Change the method declaration to public ResponseEntity<String> vehicleTest(@RequestBody List<testModel> testModel){

Comments

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.