Spring MVC mapping @RequestBody with variables containing a dot

Viewed 3565

I am doing a POST request to my Rest Contoller with the following object:

{
  "relationship.name": "someting"
}

I'd like to map that to a POJO:

public class Request {
  private String relationshipName;

  // Getters, setter and contructor
}

How would I dot that?

2 Answers

Annotate your property with @JsonProperty

public class Request {

    @JsonProperty("relationship.name")
    private String relationshipName;

    ...
}

By default, the mapping will use the variable name as the key for the property. So without the annotation, it would expect relationshipName. @JsonProperty allows you to customize the key without changing the variable name.

Have u tried using @JsonProperty on relationshipName?

@JsonProperty(name), tells Jackson ObjectMapper to map the JSON property name to the annotated Java field's name.

Related