|
1 | 1 | package com.rui.pro1.modules.sys.web.converter; |
2 | 2 |
|
| 3 | +import java.lang.annotation.Annotation; |
3 | 4 | import java.lang.reflect.Field; |
| 5 | +import java.lang.reflect.Method; |
4 | 6 | import java.util.Map; |
5 | 7 | import java.util.Set; |
6 | 8 |
|
7 | 9 | import javax.servlet.http.HttpServletRequest; |
| 10 | +import javax.validation.ConstraintViolation; |
8 | 11 | import javax.validation.ConstraintViolationException; |
9 | 12 | import javax.validation.Validator; |
10 | 13 | import javax.validation.constraints.AssertFalse; |
|
35 | 38 |
|
36 | 39 | import com.fasterxml.jackson.databind.DeserializationFeature; |
37 | 40 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 41 | +import com.rui.pro1.common.annotatiions.PermissionAnnot; |
| 42 | +import com.rui.pro1.vail.v.Car; |
38 | 43 |
|
39 | 44 | /** |
40 | 45 | * @desc 验证接口参数(方法里的实体bean和单个参数)的解析器 |
@@ -113,31 +118,54 @@ public Object resolveArgument(MethodParameter parameter, |
113 | 118 | // System.out.println(s); |
114 | 119 |
|
115 | 120 |
|
116 | | - String p=parameter.getParameterName(); |
117 | | - parameter.getParameterAnnotations(); |
118 | | - |
119 | | - Object o = BeanUtils.instantiate(parameter.getParameterType()); |
120 | | - Field[] frr = o.getClass().getDeclaredFields(); |
121 | | - // 给对象设置值 |
122 | | - BeanWrapper beanWrapper = PropertyAccessorFactory |
123 | | - .forBeanPropertyAccess(o); |
124 | | - for (Field f : frr) { |
125 | | - if (map.containsKey(f.getName())) {// 如果参数有值 |
126 | | - Object[] oValue = (Object[]) map.get(f.getName()); |
127 | | - if (oValue != null && oValue.length > 0) { |
128 | | - beanWrapper.setPropertyValue(f.getName(), oValue[0]); |
| 121 | + Annotation[] as= parameter.getMethodAnnotations(); |
| 122 | + |
| 123 | + Class decla=parameter.getDeclaringClass(); |
| 124 | + int index=parameter.getParameterIndex(); |
| 125 | + |
| 126 | + Method method=parameter.getMethod(); |
| 127 | + |
| 128 | + String pName=parameter.getParameterName(); |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + for (Annotation a : as) { |
| 133 | + if (a.annotationType() == Null.class) { |
| 134 | + if(!map.containsKey(pName)){ |
| 135 | + |
129 | 136 | } |
| 137 | + |
130 | 138 | } |
131 | 139 | } |
| 140 | + |
| 141 | + |
| 142 | + Object parameterClz=parameter.getClass(); |
| 143 | + System.out.println(parameterClz); |
| 144 | + Object containingClz=parameter.getContainingClass(); |
| 145 | + String p=parameter.getParameterName(); |
| 146 | + |
132 | 147 |
|
| 148 | + |
| 149 | + // Method method = Car.class.getMethod("drive", int.class, String.class); |
| 150 | + |
| 151 | + Object[] parameterValues = { 1, "a" }; |
| 152 | + |
| 153 | + Set<ConstraintViolation<Object>> violations = validator.forExecutables() |
| 154 | + .validateParameters(parameterClz, method, parameterValues); |
| 155 | + |
| 156 | + validator.forExecutables().validateParameters(object, method, parameterValues, groups) |
| 157 | + |
| 158 | + System.out.println(violations); |
| 159 | + for (ConstraintViolation cv : violations) { |
| 160 | + System.out.println(cv.getMessage()); |
| 161 | + } |
| 162 | + |
133 | 163 | // 验证对象 |
134 | 164 | // BeanValidators.validateWithException(validator,hello); |
135 | | - @SuppressWarnings("rawtypes") |
136 | | - Set constraintViolations = validator.validate(o); |
137 | | - if (!constraintViolations.isEmpty()) { |
138 | | - throw new ConstraintViolationException(constraintViolations); |
| 165 | + if (!violations.isEmpty()) { |
| 166 | + throw new ConstraintViolationException(violations); |
139 | 167 | } |
140 | | - return o; |
| 168 | + return null; |
141 | 169 |
|
142 | 170 | } |
143 | 171 |
|
|
0 commit comments