春にメソッドがあり、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
パラメータを正しく設定していないと推測していますが、これについてはわかりません。
この問題に 3 日以上悩まされているので、この問題を解決するのを手伝ってください。