newLearner
newLearner

Reputation: 747

How does field mapping happen for a PUT request body?

I am stuck with a basic confusion which I cannot test and cannot find a straightforward answer to as well.

I have an endpoint PUT which expects an object in the body , RequestObject. It consists of 1 field only.

class RequestObject {
 String name;
}

Now from the service from which I am hitting this endpoint, the object that I am supposed to use to send in the request has 2 fields.

class Test {
 String firstname;
 String age;
}

If I make a request, with age as null,

  1. Will this work ?
  2. Since firstName and name are not the same "spellings", will the mapping happen automatically ?

I am assuming No for both but I am not sure how to confirm.

WIll appreciate if someone can point me in right direction.

Thanks

Upvotes: 1

Views: 69

Answers (1)

mpette
mpette

Reputation: 93

@JsonIgnoreProperties(ignoreUnknown = true)
class RequestObject {

  @JsonProperty("firstname")
  String name;
}

By default Spring Boot uses the Jackson library to convert to objects. You can customize it using annotations. See https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations

Upvotes: 1

Related Questions