## sendPost method to make the POST call. It is fetching the url and also printing the data properly in the below method. ##
Public static void sendPost(String url, String data) throws Exception {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
// add header
post.setHeader("User-Agent", "Mozilla/5.0");
StringEntity requestEntity = new StringEntity(data);
post.setEntity(requestEntity);
post.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(post);
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + post.getEntity());
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.[`enter link description here`][1]append(line);
}
System.out.println(result.toString());
}
}
@RequestMapping(value="/test", method = RequestMethod.POST)
public @ResponseBody String sendTestData(@RequestBody TestDTO TestData) {
try{
log.info("Data got to ingestion rest: "+TestData);
String jsonData = new Gson().toJson(TestData).toString();
System.out.println("jsonData=="+ jsonData);
boolean result = dataIngestionHandler.insertData(jsonData);
if(result){
return "SUCCESS";
}
}catch(Exception ex) {
log.error("Error while inserting data into the db!!");
return "FAIL" + ex.getMessage();
}
return "FAIL";
}
我正在将sendPost
方法的数据发送到控制器方法,但作为响应,它正在给出:
405例外
确切的错误 代码:{“timestamp”:1467696109585,“status”:405,“error”:“方法不是 允许 “ ”异常“: ”org.springframework.web.HttpRequestMethodNotSupportedException“, ”消息“:” 请求 方法'POST'不支持“,”path“:”/ test“}。
整个设置运行正常,当我在localhost上运行数据时,数据将被插入到数据库中。但是,只要我将其推送到云端,就会出现以下异常