Reputation: 3063
Here is what i have in my controller.
@RequestMapping(value = "/accountholders/{cardHolderId}/cards/{cardId}", produces = "application/json; charset=utf-8", consumes = "application/json", method = RequestMethod.PUT)
@ResponseBody
public CardVO putCard(@PathVariable("cardHolderId") final String cardHolderId,
@PathVariable("cardId") final String cardId, @RequestBody final RequestVO requestVO) {
if (!Pattern.matches("\\d+", cardHolderId) || !Pattern.matches("\\d+", cardId)) {
throw new InvalidDataFormatException();
}
final String requestTimeStamp = DateUtil.getUTCDate();
iCardService.updateCardInfo(cardId, requestVO.isActive());
final CardVO jsonObj = iCardService.getCardHolderCardInfo(cardHolderId, cardId, requestTimeStamp);
return jsonObj;
}
This is the request body bean:-
public class RequestVO {
private boolean active;
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
The issue that I am having is when i sent the request body as {"acttttt":true} the active is set to false it updates the cardinfo with false. Whatever wrong key value i sent the active is considered as false. How would I handle this is their a way. Every other scenario is handled by spring with a 404.
Any help or suggestion is appreciated.
Upvotes: 1
Views: 2622
Reputation: 48193
Because the default value for primitive boolean
is false
. Use its corresponding Wrapper, Boolean
, instead:
public class RequestVO {
private Boolean active;
// getters and setters
}
If the active
value is required, you can also add validation annotations like NotNull
:
public class RequestVO {
@NotNull
private Boolean active;
// getters and setters
}
Then use Valid
annotation paired with RequestBody
annotation to trigger automatic validation process.
Upvotes: 2