简体   繁体   中英

how to send HTTP request through java reflection

i have a method which is developed on spring. The 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);
        }
    }

I would like to call this method through java reflection and the get the response. and the compare the response.

the tomcat is up the user is logged in to the system. while the tomcat is up i have to use the reflection to call the method.

how to do this in java reflection?

Please help and is it possible if possible please suggest me with some inputs if it is not possible support me with the possibilities which will help me to resolve the issue.

EDIT :

this is what i tried but it is not working :

Class cls;
        CreateRequisitionRO[] request = new CreateRequisitionRO[10];
        try {
            //load the HexgenWebAPI at runtime
            cls = Class.forName("com.hexgen.api.facade.HexgenWebAPI");
            Object obj = cls.newInstance();

            Method method = cls.getDeclaredMethod("createRequisition", parames,booleanVal);
            method.invoke(obj, request,true);


        }catch(Exception ex){
            ex.printStackTrace();
            System.out.println("status of reflection : "+ex.getCause());
        }

Best Regards Anto

Based on the response to the comments, you're trying to test your Spring MVC controller. I don't see any reason you should have to resolve to reflection to do that. Unit tests are best written and run outside the web container. If you want to do integration testing, check out Spring-MVC-Test , or use a browser testing framework like WebDriver or HtmlUnit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM