Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';使用http post传递请求参数_Java_Spring - Fatal编程技术网

Java Can';使用http post传递请求参数

Java Can';使用http post传递请求参数,java,spring,Java,Spring,我需要从java代码本身调用控制器。控制器如下所示: @RequestMapping(value = "temp", method = RequestMethod.POST) @ResponseBody public String uploadDataFromExcel(@RequestBody Map<String, String> colMapObj, @ModelAttribute ReqParam reqParam) { } String url ="http://loca

我需要从java代码本身调用控制器。控制器如下所示:

@RequestMapping(value = "temp", method = RequestMethod.POST)
@ResponseBody
public String uploadDataFromExcel(@RequestBody Map<String, String> colMapObj, @ModelAttribute ReqParam reqParam) {
}
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中会出现异常。 如果有人知道正确的方法,请提出建议,谢谢。

这应该行得通


URL不适用于空格。从上面的代码:“&exportDiscardRec=” 为避免此类问题,请尽可能使用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();
Map colMapObj=newhashmap();
colMapObj.put(“testKey”、“testdata”);
CloseableHttpClient=HttpClients.createDefault();
HttpPost HttpPost=新的HttpPost(url);
JSONObject body=新的JSONObject(colMapObj);
StringEntity实体=新的StringEntity(body.toString());
httpPost.setEntity(实体);
setHeader(“接受”、“应用程序/json”);
setHeader(“内容类型”、“应用程序/json”);
CloseableHttpResponse response=client.execute(httpPost);
System.out.println(response.getEntity().toString());
client.close();

更多示例只需谷歌“ApacheHTTP客户端post示例”(例如)

对查询字符串进行编码

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;
如果这不起作用,则在连接之前对单个参数进行编码

附带说明

  • 若您运行在同一个jvm中,那个么您可以直接调用该方法
  • 如果您拥有上传方法,则考虑将查询字符串更改为窗体PARAM

  • 您能提供异常堆栈吗?@Jags java.lang.IllegalArgumentException:索引78处查询中的非法字符:" . 正是在searchData=Thanks之后,您是否关心响应哪些有效,哪些无效?人们会花时间帮助你,感谢/赞赏是激励和帮助他人的好方法。@Jags很抱歉耽搁了你,事实上我在这篇文章发表了一段时间后被困在了其他地方。我现在正在尝试每一个建议。感谢Hanks@Prakash Jethava实际上angularjs中使用了相同的url,其最初目的是点击控制器。因此,itI没有问题。我尝试了上面的方法,比如String query=“reqParam.getSearchData()+”&exportDiscardRec=和String q=URLEncoder.encode(查询,“UTF-8”);然后HttpPost-HttpPost=新的HttpPost(q);但是它给了我类似org.apache.http.client.clientProtocolException这样的异常。那么我们如何将查询字符串更改为form param,您能否显示一个代码段?完整的异常是由以下原因引起的:org.apache.http.ProtocolException:未指定目标主机。@AJN不包括
    “http://localhost:8081/LeadM/tmp?"
    查询中
    变量。我更新了我的示例。谢谢@Jags。非常感谢Hanks@LenosV我使用上述方法成功地命中了所需的控制器,但我无法映射ReqParam对象。在这种情况下如何使用URIBuilder,因为该对象包含jsonarray、boolean、string和所有混合类型的数据。感谢您,我将ReqParm中的所有内容都转换为字符串。我为ReqParam对象和colMapObj使用了URIBuilder,如上所述。非常感谢@LenosV和所有人
        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();
    
    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;