2016-02-11 46 views
1

这里多余的值是我的控制器代码:豆忽略对请求主体

@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); 

} 

这是我的请求Bean: -

public class RequestVO { 
    @NotNull 
    private Boolean active; 

    public Boolean getActive() { 
     return active; 
    } 

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

我的请求体: -

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

我遇到的问题是当我在请求正文中发送一个额外的参数它似乎忽略在这种情况下额外的值“abc”。代码起作用并给我回应我需要抛出的是400 BAD REQUEST。

我错过了什么,或者是他们的方式来告诉它抛出一个额外的参数传递时的异常。

+1

你引擎盖下使用哪种JSON映射框架? – Ralph

+0

为此,我没有使用任何东西。我直接将JSON映射到Bean,@RequestBody注释对我来说就是这样。 Spring可能会在内部使用某些东西。 –

+0

因此,它可能是杰克逊2 /更快的XML – Ralph

回答

4

试试这个:

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

    public Boolean getActive() { 
     return active; 
    } 

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

提示:对于全局配置,看看这个答案:http://stackoverflow.com/a/14343479/280244 – Ralph

+0

我试过解决方案,但我仍然得到了答复。我导入了com.fasterxml.jackson.annotation.JsonIgnoreProperties。还是一样的结果。 –