2014-12-07 70 views
0

對於People資源,我將對此資源執行POST/PUT/GET/DELETE操作。資源定義如下:驗證PUT動作的資源

class People { 
    @NotBlank 
    String name; 
    @Min(18) 
    String age; 
    @NotBlank 
    @Size(min=10, max=150) 
    String address; 
} 

隨着Spring MVC和Hibernate的,

對於POST操作,我可以@Valid

但是對於PUT行動驗證資源,要求身體只包含 { 「地址」:「越南河內還劍123 Keangnan」 }

如何驗證PUT動作的資源?

回答

2

您可以使用驗證組和第一組的所有約束,例如, PeopleValidationGroup,並添加額外的地址屬性約束到第二組(名爲AddressValidationGroup)。而且你會使用@Validated註釋與適當的組指定

檢查例子http://www.javacodegeeks.com/2014/08/validation-groups-in-spring-mvc.html

@RequestMapping(value = "firstCase", method = RequestMethod.POST) 
    public String firstCase(@Validated(People.PeopleValidationGroup.class) People people, Errors errors) { 
     ... 
    } 

@RequestMapping(value = "secondCase", method = RequestMethod.PUT) 
    public String secondCase(@Validated(People.AddressValidationGroup.class) People people, Errors errors) { 
     ... 
    } 
+0

感謝主從驗證這兩種情況下,。這是我正在尋找的 – 2014-12-07 08:49:37