我需要从java代码本身调用控制器。控制器如下,
@RequestMapping(value = "temp", method = RequestMethod.POST)
@ResponseBody
public String uploadDataFromExcel(@RequestBody Map<String, String> colMapObj, @ModelAttribute ReqParam reqParam) {
}
我正在尝试使用http post调用上述控制器,如下所示,
String url ="http://localhost:8081/LeadM" + "/temp/?searchData="+ reqParam.getSearchData()+" &exportDiscardRec=" + reqParam.isExportDiscardRec() + "&fileName=" + reqParam.getFileName() + "&sheetName=" + reqParam.getSheetName() + "&importDateFormat=" + reqParam.getImportDateFormat() + "&selectedAddressTypes="+ reqParam.getSelectedAddressTypes() + "&duplicatesHandleOn=" + reqParam.getDuplicatesHandleOn() + colMapObj;
HttpPost httpPost = new HttpPost(url);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
InputStream rstream = entity.getContent();
jsonObject = new JSONObject(new JSONTokener(rstream));
其中reqParam是一个类对象是类对象,colMapObj是我想传递给上面控制器的地图。但是当执行http post时,它会在url中给出异常。 如果有人知道正确的方式,请建议,谢谢。
答案 0 :(得分:2)
这应该有效
@RequestMapping(value = "/temp", method = RequestMethod.POST)
@ResponseBody
public String uploadDataFromExcel(@RequestBody Map<String, String> colMapObj, @ModelAttribute ReqParam reqParam) {
}
和url应该是
String url ="http://localhost:8081/LeadM" + "/temp?"+ reqParam.getSearchData()+" &exportDiscardRec=" + reqParam.isExportDiscardRec() + "&fileName=" + reqParam.getFileName() + "&sheetName=" + reqParam.getSheetName() + "&importDateFormat=" + reqParam.getImportDateFormat() + "&selectedAddressTypes="+ reqParam.getSelectedAddressTypes() + "&duplicatesHandleOn=" + reqParam.getDuplicatesHandleOn() + colMapObj;
答案 1 :(得分:1)
网址不适用于spaces.From上面的代码:&#34; &安培; exportDiscardRec =&#34; 要避免此类问题,请尽可能使用 URIBuilder 或类似内容。
现在对于请求,您没有正确构建请求,例如您没有提供正文。 检查以下示例:
Map<String, String> colMapObj = new HashMap<>();
colMapObj.put("testKey", "testdata");
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
JSONObject body = new JSONObject(colMapObj);
StringEntity entity = new StringEntity(body.toString());
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
CloseableHttpResponse response = client.execute(httpPost);
System.out.println(response.getEntity().toString());
client.close();
更多示例只是google&#34; apache http客户端发布示例&#34; (例如http://www.baeldung.com/httpclient-post-http-request)
答案 2 :(得分:1)
对您的查询字符串进行编码。
String endpoint = "http://localhost:8081/LeadM/tmp?";
String query = "searchData="+ reqParam.getSearchData()+" &exportDiscardRec=" + reqParam.isExportDiscardRec() + "&fileName=" + reqParam.getFileName() + "&sheetName=" + reqParam.getSheetName() + "&importDateFormat=" + reqParam.getImportDateFormat() + "&selectedAddressTypes="+ reqParam.getSelectedAddressTypes() + "&duplicatesHandleOn=" + reqParam.getDuplicatesHandleOn() + colMapObj;
String q = URLEncoder.encode(query, "UTF-8");
String finalUrl = endpoint + q;
如果这不起作用,则在连接之前编码各个参数。
旁注