Following is my method :
@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);
}
}
In which I am able to get the following details
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)
I get the above details like the following :
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();
}
In the same way when I try to get @RequestHeader
like the following I get java.lang.NullPointerException
below is the code snippet I used
RequestHeader requestHeader = method2.getAnnotation(RequestHeader.class);
System.out.println("requestHeader : "+requestHeader.value());
what I am trying to get is @RequestHeader("validateOnly")
the value this annotations contains.
EDIT :
Thanks to @NilsH who always support for all the clarifications asked even if it required time to be spent :
this is how i solved it but the information will be available if the program is in debug mode
i have used spring to do this :
LocalVariableTableParameterNameDiscoverer lcl = new LocalVariableTableParameterNameDiscoverer();
parametersDefinedForMethod = lcl.getParameterNames(method2);
for (String params : parametersDefinedForMethod) {
System.out.println("Params : "+params);
}
Please help me to get this done.
Thanks
@RequestHeader
is a parameter level annotation in this case. Try using Method.getParameterAnnotations()
to get it.
Edit
An example:
public class MyClass {
public void someMethodWithParamAnnotations(String s, @MyAnnotation String s2) {
}
}
And then somewhere else
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