0

以下是我的方法:

@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);
                                }

请帮我完成这件事。

谢谢

4

2 回答 2

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
于 2013-04-26T09:50:06.697 回答
0

感谢@NilsH,他始终支持所有的澄清,即使需要花费时间:

这就是我解决它的方法,但如果程序处于调试模式,信息将可用

我用弹簧来做到这一点:

LocalVariableTableParameterNameDiscoverer lcl = 
    new LocalVariableTableParameterNameDiscoverer();
parametersDefinedForMethod = lcl.getParameterNames(method2);
for (String params : parametersDefinedForMethod) {
    System.out.println("Params : "+params);
}
于 2013-04-26T12:10:19.190 回答