0

我在春天有一个方法,我想使用 Apache HttpClient 访问该方法。

以下是我的方法:

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

这就是我登录系统并使 tomcat 在部署应用程序的地方运行的方式,因为该方法使用其他类来实现某些功能。

I was able to log in to the system without any issues through problematically but i get the following exception -- java.io.EOFException: No content to map to Object due to end of input

这是我的主要课程:

package com.hexgen.reflection;

public class ReflectionWebAPITest {

    public static void main(String[] args) {
        HttpClientRequests httpRequest = new HttpClientRequests();
        String uri="";
        try {
            uri = "http://localhost:8080/api/trade/createrequisition";
            httpRequest.doSubmit("mayank", "hexgen",uri); 

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}

这是我的 doSubmit 方法:

public void doSubmit(String username, String password, String uri) {
        HttpClient client = new DefaultHttpClient();
        JsonConverter jsonROConverter = new JsonConverter();
        CreateRequisitionRO[] request = new CreateRequisitionRO[1];
        BigDecimal priceFrom= new BigDecimal("100000");
        BigDecimal quantity= new BigDecimal("2");
        request[0] = new CreateRequisitionRO();
        request[0].setPortfolio("HEXGENFUND");
        request[0].setTransSrlNo(new BigDecimal(1));
        request[0].setTransCode("BUY");
        request[0].setInvestReason("009");
        request[0].setInflowOutflow(InflowOutflow.I);
        request[0].setTradeDate(new LocalDate());
        request[0].setTradeDate(new LocalDate()); 
        request[0].setTradeDateUpto(new LocalDate());
        request[0].setTradeTime("11:27:9");
        request[0].setInvestCategory("FVTPL");
        request[0].setCustodian("DEUTSCHE");
        request[0].setHoldType("HOLD");
        request[0].setSecurityType(SecurityType.INV);
        request[0].setSecurity("DABU02");
        request[0].setAssetClass("EQU");
        request[0].setIssuer("DABU");
        request[0].setMarketType(MarketType.MKT);
        request[0].setTradePriceType("");
        request[0].setRequisitionType(RequisitionType.SO);
        request[0].setPriceFrom(priceFrom);
        request[0].setPriceTo(priceFrom);
        request[0].setMarketPrice(priceFrom);
        request[0].setAveragePrice(priceFrom);
        request[0].setPrice(priceFrom);
        request[0].setQuantity(quantity);
        request[0].setGrossAmtPcy(priceFrom);
        request[0].setExchRate(quantity);
        request[0].setGrossAmtTcy(priceFrom);
        request[0].setNetAmountPcy(priceFrom);
        request[0].setNetAmountTcy(priceFrom);
        request[0].setAccrIntPcy(priceFrom);
        request[0].setAccrIntTcy(priceFrom);
        request[0].setAcquCostPcy(priceFrom);
        request[0].setYieldType(YieldType.N);
        request[0].setPurchaseYield(quantity);
        request[0].setMarketYield(quantity);
        request[0].setYtm(quantity);
        request[0].setMduration(quantity);
        request[0].setCurrPerNav(quantity);
        request[0].setDesiredPerNav(quantity);
        request[0].setCurrHolding(quantity);
        request[0].setNoofDays(quantity);
        request[0].setRealGlTcy(quantity);
        request[0].setRealGlPcy(quantity);
        request[0].setNowLater("N");
        request[0].setIsAllocable(false); 
        request[0].setAcquCostReval(quantity);
        request[0].setAcquCostHisTcy(quantity);
        request[0].setAcquCostHisPcy(quantity);
        request[0].setExIntPcy(quantity);
        request[0].setExIntTcy(quantity);;
        request[0].setAccrIntReval(quantity);
        request[0].setAccrIntTcy(quantity);
        request[0].setAccrIntPcy(quantity);;
        request[0].setGrossAodTcy(quantity);
        request[0].setGrossAodPcy(quantity);
        request[0].setGrossAodReval(quantity);
        request[0].setBankAccAmtAcy(quantity);
        request[0].setBankAccAmtPcy(quantity);
        request[0].setTaxAmountTcy(quantity);
        request[0].setUnrelAmortPcy(quantity);
        request[0].setUnrelAmortTcy(quantity);
        request[0].setUnrelGlPcy(quantity);
        request[0].setUnrelGlTcy(quantity);
        request[0].setRealGlHisTcy(quantity);
        request[0].setRealGlHisPcy(quantity);
        request[0].setTradeFeesTcy(quantity);
        request[0].setTradeFeesPcy(quantity);

        boolean validateOnly = true;
        HttpPost post = new HttpPost("http://localhost:8080/j_spring_security_check");

        try {

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("j_username", username));
            nameValuePairs.add(new BasicNameValuePair("j_password", password));

            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = client.execute(post);

            HttpEntity entity = response.getEntity();

            if (entity != null) {
                post.abort();
            }

            HttpPost postURI = new HttpPost(uri);
            // Setup the request parameters
                BasicHttpParams params = new BasicHttpParams();
                params.setParameter("CreateRequisitionRO", jsonROConverter.serialiseRequisionRO(request));
                params.setBooleanParameter("validateOnly", validateOnly);
                postURI.setParams(params);
            postURI.setHeader("Content-type", "application/json");

            HttpResponse responseURL = client.execute(postURI);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

这些是我用来将其转换Java POJO为 Json 的方法;

public JSONArray serialiseRequisionRO(CreateRequisitionRO[] requisitionRO) {
        JSONSerializer serializer = new JSONSerializer();

        List<String> requisitionROList = new ArrayList<String>();

        for(int i = 0 ; i< requisitionRO.length ; i++)
        {
            requisitionROList.add(serializer.serialize(requisitionRO[i]));
        }
        System.out.println("JSON : "+serializer.serialize(requisitionRO[0]));
        return convertListToJSON(requisitionROList);
    }
    @SuppressWarnings("unchecked")
    public JSONArray convertListToJSON(List<String> requisitionROList){
        JSONArray requestCollection = new JSONArray();
        JsonFactory jsonFactory = new JsonFactory();
        for(int i = 0 ; i< requisitionROList.size() ; i++)
        { 
            requestCollection.add(requisitionROList.get(i));
        }

        return requestCollection;
    }

在做了所有这些之后i get the following exception -- java.io.EOFException: No content to map to Object due to end of input

我猜测我没有正确设置参数,但我不确定。

请帮助我解决这个问题,因为我已经为此苦苦挣扎了三天多。

4

1 回答 1

0

我得到了上面java.io.EOFException: No content to map to Object due to end of input的原因是因为调用需要一些参数,因为我已经发送了没有参数的请求。这也可能发生在参数将 json 数组作为参数但我发送的 json 字符串也给我同样的异常。

于 2013-04-26T05:01:23.600 回答