1

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Another issue that I am having now is the when i sent {"active":2343} its taking it as true I think its considering 0 as false and everything else as true is their a way to check resolve that not to consider that.
I guess you could write your own Converter and add it to list of converters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.