以下是我的方法:
@PreAuthorize("isAuthenticated() and hasPermission(#request, 'CREATE_REQUISITION')")
@RequestMapping(method = RequestMethod.POST, value = "/trade/createrequisition")
public @ResponseBody
void createRequisition(@RequestBody CreateRequisitionRO[] request,
@RequestHeader("validateOnly") boolean validateOnly) {
logger.debug("Starting createRequisition()...");
for (int i = 0; i < request.length; i++) {
CreateRequisitionRO requisitionRequest = request[i];
// FIXME this has to be removed/moved
requisitionRequest.setFundManager(requisitionRequest.getUserId());
// FIXME might have to search using param level as well
SystemDefault sysDefault = dbFuncs.references.systemDefault
.findByCompanyAndDivisionAndPortfolio(
userContext.getCompany(),
userContext.getDivision(),
requisitionRequest.getPortfolio());
requisitionRequest.setCustodianN(sysDefault.getCustodianN());
gateKeeper.route(requisitionRequest);
}
}
在其中我可以获得以下详细信息
1. @RequestMapping(method = RequestMethod.POST, value = "/trade/createrequisition")
2. void createRequisition(@RequestBody CreateRequisitionRO[] request,
@RequestHeader("validateOnly") boolean validateOnly)
(in thesecond one i am able to get the argument type like boolean etc)
我得到以上细节,如下所示:
Class cls;
cls = Class.forName(obj.getName());
Method[] method = cls.getDeclaredMethods();
for (Method method2 : method) {
RequestMapping requestMappingAnnotation = method2.getAnnotation(RequestMapping.class); // gets the method which is maped with RequestMapping Annotation
requestMappingValues = requestMappingAnnotation.value(); // to get the url value
RequestMethod[] methods = requestMappingAnnotation.method(); // to get the request method type
requestingMethod = methods[0].name();
}
以相同的方式,当我尝试获得如下所示的
@RequestHeader
时,我得到了java.lang.NullPointerException
以下是我使用的代码段
RequestHeader requestHeader = method2.getAnnotation(RequestHeader.class);
System.out.println("requestHeader : "+requestHeader.value());
我想要得到的是
@RequestHeader("validateOnly")
此批注包含的值。编辑:
感谢@NilsH,他始终支持所有要求的澄清,即使需要花费时间:
这就是我解决它的方式
but the information will be available if the program is in debug mode
我已经用spring来做到这一点:
LocalVariableTableParameterNameDiscoverer lcl = new LocalVariableTableParameterNameDiscoverer();
parametersDefinedForMethod = lcl.getParameterNames(method2);
for (String params : parametersDefinedForMethod) {
System.out.println("Params : "+params);
}
请帮助我完成此任务。
谢谢
最佳答案
在这种情况下,@RequestHeader
是参数级别的注释。尝试使用Method.getParameterAnnotations()
来获取它。
编辑
一个例子:
public class MyClass {
public void someMethodWithParamAnnotations(String s, @MyAnnotation String s2) {
}
}
然后在其他地方
Method m = MyClass.class.getMethod("someMethodWithParamAnnotations", String.class, String.class);
Annotation[][] paramAnnotations = m.getParameterAnnotations();
Annotation[] firstParamAnnotation = paramAnnotations[0];
// Above is empty array, since first parameter has no annotation
Annotation[] secondParamAnnotation = paramAnnotations[1];
// Above contains an array with the `@MyAnnotation` annotation