spring mvc 4 校验
一、controller中添加:
@Resource
GatewayValidator gatewayValidator;
@RequestMapping(value = "/gateway/signup", method = RequestMethod.POST) public ApiResponse signup(@Valid Gateway gateway, BindingResult result) { ApiResponse rspResult = new ApiResponse(); gatewayValidator.validate(gateway, result); if(result.hasErrors()){ rspResult.setCode(ApiCode.PARMAS_ERROR); rspResult.setMessage(BaseUtils.GetValidErrorMessage(result)); } else { //do something } return rspResult; }
二、model中添加(简单的校验可以直接卸载model类中):
package smarthome.api.models; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.apache.commons.lang3.time.DateUtils; import org.hibernate.validator.constraints.NotEmpty; import java.io.Serializable; import java.util.ArrayList; import java.util.Date; import java.util.List; public class Gateway extends BaseClass implements Serializable { /** * None */ protected String vendorCode; /** * None */ protected String productCode; /** * None */ @NotEmpty(message = "序列号不能为空") protected String serialNumber; public Gateway() { } public String getVendorCode() { return vendorCode; } public void setVendorCode(String vendorCode) { this.vendorCode = vendorCode == null ? null : vendorCode.trim(); } public String getProductCode() { return productCode; } public void setProductCode(String productCode) { this.productCode = productCode == null ? null : productCode.trim(); } public String getSerialNumber() { return serialNumber; } public void setSerialNumber(String serialNumber) { this.serialNumber = serialNumber == null ? null : serialNumber.trim(); } }
三、简单的校验
package smarthome.api.validators; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; import org.springframework.validation.Validator; import javax.annotation.Resource; import java.util.List; @Component public class GatewayValidator implements Validator { public boolean supports(Class clazz) { return Gateway.class.equals(clazz); } public void validate(Object target, Errors errors) { Gateway gateway = (Gateway) target; if(gateway.getGatewayId() == null && StringUtils.isEmpty(gateway.getSerialNumber())){ errors.rejectValue("gatewayId", null, "网关编号和序列号不能同时为空"); errors.rejectValue("serialNumber", null, "网关编号和序列号不能同时为空"); } } }
四、嵌套的校验
1、父类(gateway)中添加校验
package smarthome.api.validators; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; import org.springframework.validation.Validator; import javax.annotation.Resource; import java.util.List; @Component public class GatewayValidator implements Validator { @Resource private ModuleValidator moduleValidator; public boolean supports(Class clazz) { return Gateway.class.equals(clazz); } public void validate(Object target, Errors errors) { Gateway gateway = (Gateway) target; if(gateway.getGatewayId() == null && StringUtils.isEmpty(gateway.getSerialNumber())){ errors.rejectValue("gatewayId", null, "网关编号和序列号不能同时为空"); errors.rejectValue("serialNumber", null, "网关编号和序列号不能同时为空"); } for(Module module : (List<Module>)gateway.getModuleList()){ ValidationUtils.invokeValidator(moduleValidator, module, errors); } } }
2、子类(module)中的校验
package smarthome.api.validators; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; import org.springframework.validation.Validator; import javax.annotation.Resource; @Component public class ModuleValidator implements Validator { @Resource private IModuleService moduleService; public boolean supports(Class clazz) { return Module.class.equals(clazz); } public void validate(Object target, Errors errors) { Module module = (Module) target; if (StringUtils.isEmpty(module.getSerialNumber())) { errors.rejectValue("serialNumber", null, "模块序号不能为空"); } else { if(moduleService.getFirst(module.getSerialNumber()) != null){ errors.rejectValue("serialNumber", null, "模块序列号已经存在"); } } } }