简体   繁体   中英

Bean Ignoring the extra values on the request Body

Here is my Controller code:

@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, @Valid @RequestBody final RequestVO requestVO,
        @RequestParam final Map<String, String> allRequestParams) {
    iCardService.updateCardInfo(cardId, requestVO.getActive());
    return iCardService.getCardHolderCardInfo(cardHolderId, cardId);

}

THis is my Request Bean:-

public class RequestVO {
    @NotNull
    private Boolean active;

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

My request Body:-

{
    "active":true,
    "abc":"ignoring this"
}

The problem I am having is when i sent an extra parameter in the request Body It seems to ignore the extra value in this case "abc". The code works and give me the response What I need to throw is 400 BAD REQUEST.

Am i missing something or is their a way to tell it to throw an exception when an extra parameter is passed.

Try this:

@JsonIgnoreProperties(ignoreUnknown = false)
public class RequestVO {
    @NotNull
    private Boolean active;

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM