以下是我的方法:
@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
我用春天来做这件事:
LocalVariableTableParameterNameDiscoverer lcl = new LocalVariableTableParameterNameDiscoverer();
parametersDefinedForMethod = lcl.getParameterNames(method2);
for (String params : parametersDefinedForMethod) {
System.out.println("Params : "+params);
}
请帮我完成这件事。
谢谢
答案 0 :(得分:2)
@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
答案 1 :(得分:0)
感谢@NilsH,即使需要花时间,也始终支持所有澄清:
这就是我解决它的方法,但如果程序处于调试模式,信息将可用
我用春天来做这件事:
LocalVariableTableParameterNameDiscoverer lcl =
new LocalVariableTableParameterNameDiscoverer();
parametersDefinedForMethod = lcl.getParameterNames(method2);
for (String params : parametersDefinedForMethod) {
System.out.println("Params : "+params);
}