当我在DTO中有一些读/写访问字段时,@ Valid产生错误

时间:2017-07-13 06:48:29

标签: spring rest bean-validation

您好,

我有以下两个要求由我的DTO提供服务:

1. PATCH请求更新值:

{ 
    "email": "test@xyz.com",  
    "address" : "Xyz street, 4th cross, first main", 
    "city" : "Bangalore",
    "state" : "Karnataka",
    "pinCode": 560078,
    "creditDebitLimit": 5000.00,
    "amountPref":"###,###",
    "datePref": "mm/dd/yy",
    "userId":  "user_Id"
    }

2.获取查看值的请求:

{
    "custId":12345
    "firstName": "Test",
    "lastName" : "Test" ,
    "aadharId" : "704123432122",
    "email": "test@xyz.com",  
    "address" : "Xyz street, 4th cross, first main", 
    "city" : "Bangalore",
    "state" : "Karnataka",
    "pinCode": 560078,
    "creditDebitLimit":5000.00
    "amountPref":lakhs"
    "datePref":"mm/dd/yy"
}

DTO课程:

public class UpdateCustomerDTO implements Serializable {

    @JsonProperty(value="cust",access=Access.READ_ONLY)
    private int custId;

    @JsonProperty(value="firstName",access=Access.READ_ONLY)
    private String firstName;

    @JsonProperty(value="lastName",access=Access.READ_ONLY)
    private String lastName;

    @JsonProperty(value="aadharId",access=Access.READ_ONLY)
    private String aadharId;

    @NotBlank(message="updatecustomer.email.mandatory")
    @Email(message="updatecustomer.email.valid")
    @JsonProperty("email")
    private String emailId;

    @NotBlank(message="updatecustomer.address.mandatory")
    @JsonProperty("address")
    private String address;

    @NotBlank(message="updatecustomer.city.mandatory")
    @JsonProperty("city")
    private String city;

    @NotBlank(message="updatecustomer.state.mandatory")
    @JsonProperty("state")
    private String state;

    @NotBlank(message="updatecustomer.pincode.mandatory")
    @JsonProperty("pinCode")
    private String pincode;

    @NotBlank(message="updatecustomer.creditnotification.size")
    @Digits(integer=8,fraction=4,message="newcustomer.creditnotification.size")
    @JsonProperty("creditDebitLimit")
    private BigDecimal crDbNotifLimit;

    @NotBlank(message="updatecustomer.amountPref.mandatory")
    @JsonProperty("amountPref")
    private String amountPref;

    @NotBlank(message = "updatecustomer.datePref.mandatory")
    @JsonProperty("datePref")
    private String datePref;

    @NotBlank(message = "updatecustomer.lastUpdatedId.mandatory")
    @JsonProperty(value="userId",access = Access.WRITE_ONLY)
    private String lstUpdtId;

}

控制器:

@RequestMapping(value = "/customers/{custId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public UpdateCustomerDTO viewProfile(@PathVariable int custId){
    UpdateCustomerDTO customerDTO = customerService.viewCustomerProfile(custId);
    if(customerDTO != null){

        return customerDTO;
    }
}

@RequestMapping(value="/customers/{custId}", method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_JSON_VALUE)
public boolean updateProfile(@Valid @RequestBody UpdateCustomerDTO updateCustomerDTO,@PathVariable int custId){
    return customerService.updateCustomerProfile(updateCustomerDTO,custId);
}

GET部分按预期工作,但PATCH请求抛出错误:

{
  "errorCode": 500,
  "errorMsg": "An unknown error occured."
}
  

但是当我从PATCH控制器中删除@Valid注释时   一切正常,但无法验证。

0 个答案:

没有答案