nassim nassim - 18 days ago 9
JSON Question

How to get JSON Object, REST API, Jhipster, SpringBoot

I'm coding an API using Jhipster. Server side is programmed with SpringBot. I want to get JSON Object that i send with PostMan

{
"user" : {
"name" : "name",
"surname": "surname"
}
}


I create a ressource and a class to get this JSON Object

@RequestMapping(value = "/",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<AlertBalance> create(@RequestBody User user) throws URISyntaxException {

System.out.println("name "+ user.getName()+"/ surname : "+User.getSurname());

}


User class Code

@JsonIgnoreProperties(ignoreUnknown = true)
public class User implements Serializable {

private String name;
private String surname ;
@JsonCreator
public User(@JsonProperty("surname") String surname , @JsonProperty("name") String name){
this.surname = surname;
this.name = name;
}


public User(){
}
//setters and getters
}


The create() method is called when I do request from postman, but the value of name and surname is null. Do you know what I can do to get the real values ?

Answer

I just find the mistake, the JSON Object I sent was incompatible. I change it with

{
    "name" : "name",
    "surname": "surname"
}

and now it works.

Comments