@Test(expected = BindException.class) public void testResolveArgumentWithValidationError() throws Exception { initializer.setValidator(new AlwaysFailValidator()); request.setMethod("put"); request.setRequestURI("/opentides/ninja/1"); request.addParameter("firstName", "Master New"); request.addParameter("lastName", "Splinter New"); when(beanFactory.getBean(NinjaCrudController.class)).thenReturn(controller); Ninja ninja = new Ninja(); ninja.setId(1l); ninja.setFirstName("Master"); ninja.setLastName("Splinter"); when(ninjaService.load("1")).thenReturn(ninja); resolver.resolveArgument(param1, mvc, nwr, dataBinderFactory); }
@SuppressWarnings("unchecked") public final Object resolveArgument( MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest nativeRequest, WebDataBinderFactory binderFactory) throws Exception { FormBind annot = parameter.getParameterAnnotation(FormBind.class); Class<?> clazz = parameter.getDeclaringClass(); String name = getName(annot, parameter); HttpServletRequest request = (HttpServletRequest) nativeRequest.getNativeRequest(); Method addForm = CacheUtil.getNewFormBindMethod(clazz); Object controller = beanFactory.getBean(parameter.getContainingClass()); Object target = (addForm!=null) ? addForm.invoke(controller, request) : BeanUtils.instantiateClass(parameter.getParameterType()); MutablePropertyValues mpvs = new MutablePropertyValues(nativeRequest.getParameterMap()); WebDataBinder binder = binderFactory.createBinder(nativeRequest, target, name); if (binder.getTarget() != null) { binder.bind(mpvs); if (binder.getValidator()!= null) binder.validate(); if (binder.getBindingResult().hasErrors()) { throw new BindException(binder.getBindingResult()); } String method = request.getMethod().toLowerCase(); String uri = request.getRequestURI(); String sid = uri.substring(uri.lastIndexOf("/")+1); Long id = StringUtil.convertToLong(sid, 0); if ( ("put".equals(method) || "post".equals(method)) && id > 0 && BaseEntity.class.isAssignableFrom(parameter.getParameterType()) ) { Method updateForm = CacheUtil.getUpdateFormBindMethod(clazz); BaseEntity record = null; if (updateForm==null) { Method getService = controller.getClass().getMethod("getService"); if (getService == null) { String message = "Cannot find method with @FormBind with update mode. " + "Also, unable to find service associated to controller." + "Please specify one that retrieves record from database."; throw new InvalidImplementationException(message); } BaseCrudService<? extends BaseEntity> service = (BaseCrudService<? extends BaseEntity>) getService.invoke(controller); record = (BaseEntity) service.load(sid); } else { record = (BaseEntity) updateForm.invoke(controller, sid, request); } if (record != null) { WebDataBinder updateBinder = binderFactory.createBinder(nativeRequest, record, name); updateBinder.bind(mpvs); mavContainer.addAllAttributes(updateBinder.getBindingResult().getModel()); return updateBinder.getTarget(); } else { String message = "Unable to find "+parameter.getParameterType().getSimpleName()+" with id="+sid+" for update."; throw new DataAccessException(message); } } else if ( "post".equals(method) ) { mavContainer.addAllAttributes(binder.getBindingResult().getModel()); return binder.getTarget(); } else { throw new InvalidImplementationException("@FormBind argument annotation can only be used on POST or PUT methods."); } } mavContainer.addAllAttributes(binder.getBindingResult().getModel()); return binder.getTarget(); }
FormBindMethodProcessor implements HandlerMethodArgumentResolver { @SuppressWarnings("unchecked") public final Object resolveArgument( MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest nativeRequest, WebDataBinderFactory binderFactory) throws Exception { FormBind annot = parameter.getParameterAnnotation(FormBind.class); Class<?> clazz = parameter.getDeclaringClass(); String name = getName(annot, parameter); HttpServletRequest request = (HttpServletRequest) nativeRequest.getNativeRequest(); Method addForm = CacheUtil.getNewFormBindMethod(clazz); Object controller = beanFactory.getBean(parameter.getContainingClass()); Object target = (addForm!=null) ? addForm.invoke(controller, request) : BeanUtils.instantiateClass(parameter.getParameterType()); MutablePropertyValues mpvs = new MutablePropertyValues(nativeRequest.getParameterMap()); WebDataBinder binder = binderFactory.createBinder(nativeRequest, target, name); if (binder.getTarget() != null) { binder.bind(mpvs); if (binder.getValidator()!= null) binder.validate(); if (binder.getBindingResult().hasErrors()) { throw new BindException(binder.getBindingResult()); } String method = request.getMethod().toLowerCase(); String uri = request.getRequestURI(); String sid = uri.substring(uri.lastIndexOf("/")+1); Long id = StringUtil.convertToLong(sid, 0); if ( ("put".equals(method) || "post".equals(method)) && id > 0 && BaseEntity.class.isAssignableFrom(parameter.getParameterType()) ) { Method updateForm = CacheUtil.getUpdateFormBindMethod(clazz); BaseEntity record = null; if (updateForm==null) { Method getService = controller.getClass().getMethod("getService"); if (getService == null) { String message = "Cannot find method with @FormBind with update mode. " + "Also, unable to find service associated to controller." + "Please specify one that retrieves record from database."; throw new InvalidImplementationException(message); } BaseCrudService<? extends BaseEntity> service = (BaseCrudService<? extends BaseEntity>) getService.invoke(controller); record = (BaseEntity) service.load(sid); } else { record = (BaseEntity) updateForm.invoke(controller, sid, request); } if (record != null) { WebDataBinder updateBinder = binderFactory.createBinder(nativeRequest, record, name); updateBinder.bind(mpvs); mavContainer.addAllAttributes(updateBinder.getBindingResult().getModel()); return updateBinder.getTarget(); } else { String message = "Unable to find "+parameter.getParameterType().getSimpleName()+" with id="+sid+" for update."; throw new DataAccessException(message); } } else if ( "post".equals(method) ) { mavContainer.addAllAttributes(binder.getBindingResult().getModel()); return binder.getTarget(); } else { throw new InvalidImplementationException("@FormBind argument annotation can only be used on POST or PUT methods."); } } mavContainer.addAllAttributes(binder.getBindingResult().getModel()); return binder.getTarget(); } }
FormBindMethodProcessor implements HandlerMethodArgumentResolver { @SuppressWarnings("unchecked") public final Object resolveArgument( MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest nativeRequest, WebDataBinderFactory binderFactory) throws Exception { FormBind annot = parameter.getParameterAnnotation(FormBind.class); Class<?> clazz = parameter.getDeclaringClass(); String name = getName(annot, parameter); HttpServletRequest request = (HttpServletRequest) nativeRequest.getNativeRequest(); Method addForm = CacheUtil.getNewFormBindMethod(clazz); Object controller = beanFactory.getBean(parameter.getContainingClass()); Object target = (addForm!=null) ? addForm.invoke(controller, request) : BeanUtils.instantiateClass(parameter.getParameterType()); MutablePropertyValues mpvs = new MutablePropertyValues(nativeRequest.getParameterMap()); WebDataBinder binder = binderFactory.createBinder(nativeRequest, target, name); if (binder.getTarget() != null) { binder.bind(mpvs); if (binder.getValidator()!= null) binder.validate(); if (binder.getBindingResult().hasErrors()) { throw new BindException(binder.getBindingResult()); } String method = request.getMethod().toLowerCase(); String uri = request.getRequestURI(); String sid = uri.substring(uri.lastIndexOf("/")+1); Long id = StringUtil.convertToLong(sid, 0); if ( ("put".equals(method) || "post".equals(method)) && id > 0 && BaseEntity.class.isAssignableFrom(parameter.getParameterType()) ) { Method updateForm = CacheUtil.getUpdateFormBindMethod(clazz); BaseEntity record = null; if (updateForm==null) { Method getService = controller.getClass().getMethod("getService"); if (getService == null) { String message = "Cannot find method with @FormBind with update mode. " + "Also, unable to find service associated to controller." + "Please specify one that retrieves record from database."; throw new InvalidImplementationException(message); } BaseCrudService<? extends BaseEntity> service = (BaseCrudService<? extends BaseEntity>) getService.invoke(controller); record = (BaseEntity) service.load(sid); } else { record = (BaseEntity) updateForm.invoke(controller, sid, request); } if (record != null) { WebDataBinder updateBinder = binderFactory.createBinder(nativeRequest, record, name); updateBinder.bind(mpvs); mavContainer.addAllAttributes(updateBinder.getBindingResult().getModel()); return updateBinder.getTarget(); } else { String message = "Unable to find "+parameter.getParameterType().getSimpleName()+" with id="+sid+" for update."; throw new DataAccessException(message); } } else if ( "post".equals(method) ) { mavContainer.addAllAttributes(binder.getBindingResult().getModel()); return binder.getTarget(); } else { throw new InvalidImplementationException("@FormBind argument annotation can only be used on POST or PUT methods."); } } mavContainer.addAllAttributes(binder.getBindingResult().getModel()); return binder.getTarget(); }  }
FormBindMethodProcessor implements HandlerMethodArgumentResolver { @SuppressWarnings("unchecked") public final Object resolveArgument( MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest nativeRequest, WebDataBinderFactory binderFactory) throws Exception { FormBind annot = parameter.getParameterAnnotation(FormBind.class); Class<?> clazz = parameter.getDeclaringClass(); String name = getName(annot, parameter); HttpServletRequest request = (HttpServletRequest) nativeRequest.getNativeRequest(); Method addForm = CacheUtil.getNewFormBindMethod(clazz); Object controller = beanFactory.getBean(parameter.getContainingClass()); Object target = (addForm!=null) ? addForm.invoke(controller, request) : BeanUtils.instantiateClass(parameter.getParameterType()); MutablePropertyValues mpvs = new MutablePropertyValues(nativeRequest.getParameterMap()); WebDataBinder binder = binderFactory.createBinder(nativeRequest, target, name); if (binder.getTarget() != null) { binder.bind(mpvs); if (binder.getValidator()!= null) binder.validate(); if (binder.getBindingResult().hasErrors()) { throw new BindException(binder.getBindingResult()); } String method = request.getMethod().toLowerCase(); String uri = request.getRequestURI(); String sid = uri.substring(uri.lastIndexOf("/")+1); Long id = StringUtil.convertToLong(sid, 0); if ( ("put".equals(method) || "post".equals(method)) && id > 0 && BaseEntity.class.isAssignableFrom(parameter.getParameterType()) ) { Method updateForm = CacheUtil.getUpdateFormBindMethod(clazz); BaseEntity record = null; if (updateForm==null) { Method getService = controller.getClass().getMethod("getService"); if (getService == null) { String message = "Cannot find method with @FormBind with update mode. " + "Also, unable to find service associated to controller." + "Please specify one that retrieves record from database."; throw new InvalidImplementationException(message); } BaseCrudService<? extends BaseEntity> service = (BaseCrudService<? extends BaseEntity>) getService.invoke(controller); record = (BaseEntity) service.load(sid); } else { record = (BaseEntity) updateForm.invoke(controller, sid, request); } if (record != null) { WebDataBinder updateBinder = binderFactory.createBinder(nativeRequest, record, name); updateBinder.bind(mpvs); mavContainer.addAllAttributes(updateBinder.getBindingResult().getModel()); return updateBinder.getTarget(); } else { String message = "Unable to find "+parameter.getParameterType().getSimpleName()+" with id="+sid+" for update."; throw new DataAccessException(message); } } else if ( "post".equals(method) ) { mavContainer.addAllAttributes(binder.getBindingResult().getModel()); return binder.getTarget(); } else { throw new InvalidImplementationException("@FormBind argument annotation can only be used on POST or PUT methods."); } } mavContainer.addAllAttributes(binder.getBindingResult().getModel()); return binder.getTarget(); }  boolean supportsParameter(MethodParameter parameter); @SuppressWarnings("unchecked") final Object resolveArgument(
			MethodParameter parameter, ModelAndViewContainer mavContainer,
			NativeWebRequest nativeRequest, WebDataBinderFactory binderFactory); }
FormBindMethodProcessor implements HandlerMethodArgumentResolver { @SuppressWarnings("unchecked") public final Object resolveArgument( MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest nativeRequest, WebDataBinderFactory binderFactory) throws Exception { FormBind annot = parameter.getParameterAnnotation(FormBind.class); Class<?> clazz = parameter.getDeclaringClass(); String name = getName(annot, parameter); HttpServletRequest request = (HttpServletRequest) nativeRequest.getNativeRequest(); Method addForm = CacheUtil.getNewFormBindMethod(clazz); Object controller = beanFactory.getBean(parameter.getContainingClass()); Object target = (addForm!=null) ? addForm.invoke(controller, request) : BeanUtils.instantiateClass(parameter.getParameterType()); MutablePropertyValues mpvs = new MutablePropertyValues(nativeRequest.getParameterMap()); WebDataBinder binder = binderFactory.createBinder(nativeRequest, target, name); if (binder.getTarget() != null) { binder.bind(mpvs); if (binder.getValidator()!= null) binder.validate(); if (binder.getBindingResult().hasErrors()) { throw new BindException(binder.getBindingResult()); } String method = request.getMethod().toLowerCase(); String uri = request.getRequestURI(); String sid = uri.substring(uri.lastIndexOf("/")+1); Long id = StringUtil.convertToLong(sid, 0); if ( ("put".equals(method) || "post".equals(method)) && id > 0 && BaseEntity.class.isAssignableFrom(parameter.getParameterType()) ) { Method updateForm = CacheUtil.getUpdateFormBindMethod(clazz); BaseEntity record = null; if (updateForm==null) { Method getService = controller.getClass().getMethod("getService"); if (getService == null) { String message = "Cannot find method with @FormBind with update mode. " + "Also, unable to find service associated to controller." + "Please specify one that retrieves record from database."; throw new InvalidImplementationException(message); } BaseCrudService<? extends BaseEntity> service = (BaseCrudService<? extends BaseEntity>) getService.invoke(controller); record = (BaseEntity) service.load(sid); } else { record = (BaseEntity) updateForm.invoke(controller, sid, request); } if (record != null) { WebDataBinder updateBinder = binderFactory.createBinder(nativeRequest, record, name); updateBinder.bind(mpvs); mavContainer.addAllAttributes(updateBinder.getBindingResult().getModel()); return updateBinder.getTarget(); } else { String message = "Unable to find "+parameter.getParameterType().getSimpleName()+" with id="+sid+" for update."; throw new DataAccessException(message); } } else if ( "post".equals(method) ) { mavContainer.addAllAttributes(binder.getBindingResult().getModel()); return binder.getTarget(); } else { throw new InvalidImplementationException("@FormBind argument annotation can only be used on POST or PUT methods."); } } mavContainer.addAllAttributes(binder.getBindingResult().getModel()); return binder.getTarget(); }  boolean supportsParameter(MethodParameter parameter); @SuppressWarnings("unchecked") final Object resolveArgument(
			MethodParameter parameter, ModelAndViewContainer mavContainer,
			NativeWebRequest nativeRequest, WebDataBinderFactory binderFactory);  }