Home > Blockchain >  Make one property in request body not required
Make one property in request body not required

Time:01-06

I have the following interface:

public interface TestRequestView {
  String getCountryCode();
  String getRegionCode();
}

It's used in several end points like so:

@PostMapping("/my/path/{id}")
public String test(@RequestBody TestRequestView request, @PathVariable String id) {
 ...
} 

I would like to add a property to the TestRequestView that is only used in one new endpoint without breaking the rest, how can I mark that one property as ignorable? Something like:

public interface TestRequestView {
  String getCountryCode();
  String getRegionCode();
  String getEmail(); // make this not required
}

CodePudding user response:

  1. Usually it is better to use 1 such a model per endpoint so they are independent. If you share models between endpoints this should be useful

  2. This may help

CodePudding user response:

You can use the @JsonIgnoreProperties annotation

Example:

@JsonIgnoreProperties(value = {"email"})
public interface TestRequestView {
  String getCountryCode();
  String getRegionCode();

  @JsonProperty(required = false)
  String getEmail();
}

CodePudding user response:

Add annotation @JsonInclude(Include.NON_NULL) at class level for your TestRequestView. When passing your param you can pass it without that value and in your controller it will be received with that param as null. You just need to make sure that your controller can handle such case

  • Related