如何使用java Resteplate将原始数据添加到body请求
如何使用JavaREST模板添加原始数据(如以下示例)作为正文请求如何使用java Resteplate将原始数据添加到body请求,java,resttemplate,Java,Resttemplate,如何使用JavaREST模板添加原始数据(如以下示例)作为正文请求 { "body": { "content": "This is sent via postman to MS-team general channel" } } 我在使用JavaREST模板发送请求体中的原始数据时遇到了困难,所以我在这里添加此代码以供将来参考(我是java编码的新手) 以下代码适用于我向MS团队频道发送消息 public JS
{
"body": {
"content": "This is sent via postman to MS-team general channel"
}
}
我在使用JavaREST模板发送请求体中的原始数据时遇到了困难,所以我在这里添加此代码以供将来参考(我是java编码的新手) 以下代码适用于我向MS团队频道发送消息
public JSONObject sendMessage(String team_ID, String channel_ID){
JSONObject res =null;
RestTemplate restTemplate = new RestTemplate();
String url="https://graph.microsoft.com/beta/teams/"+team_ID+"/channels/"+channel_ID+"/messages";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
String token = "xxxxxxxxxx";
headers.set("Authorization","Bearer "+token);
// This didn't worked
/*MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
map.add("content", "Welcome to General channel message sent via intellij IDE");
MultiValueMap<String, String> map1= new LinkedMultiValueMap<String, String>();
map1.add("content", "Welcome to General channel message sent via intellij IDE");
//map.add("body", "Welcome to General channel message sent via intellij IDE");*/
// This one also didn't worked
//JSONObject jsonObject = new JSONObject();
//jsonObject.put("body", "{ \"body\": {\"content\":\"Welcome to General channel message sent via intellij IDE using java coding & http rest-Template\"}}");
// This one also didn't worked
//JSONObject jsonObject = new JSONObject();
//jsonObject.put("body", "{\"content\":\"Welcome to General channel message sent via intellij IDE using java coding & http rest-Template\"}");
// This one worked
String requestJson = "{ \"body\": {\"content\":\"Welcome to General channel message sent via intellij IDE using java coding & http rest-Template\"}}";
// HttpEntity<String> request = new HttpEntity<String>(jsonObject.toString(),headers);
HttpEntity<String> request = new HttpEntity<String>(requestJson,headers);
ResponseEntity<String> response=null;
try {
response = restTemplate.postForEntity(url, request, String.class);
res= new JSONObject(response.getBody());
System.out.println(res);
}catch(Exception e){
e.printStackTrace();
}
return res;
}
publicJSONObject发送消息(字符串团队ID、字符串通道ID){
JSONObject res=null;
RestTemplate RestTemplate=新RestTemplate();
字符串url=”https://graph.microsoft.com/beta/teams/“+team_ID+”/channels/“+channel_ID+”/messages”;
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
字符串标记=“XXXXXXXXX”;
headers.set(“授权”、“承载人”+令牌);
//这不管用
/*MultiValueMap=新链接的MultiValueMap();
添加(“内容”,“欢迎通过intellij IDE发送到通用频道消息”);
MultiValueMap map1=新链接的MultiValueMap();
添加(“内容”,“欢迎通过intellij IDE发送到通用频道消息”);
//添加(“正文”,“欢迎通过intellij IDE发送通用频道消息”)*/
//这个也没用
//JSONObject JSONObject=新的JSONObject();
//put(“body”、“{\“body\”:{\“content\”:\“欢迎使用java编码和http rest模板通过intellij IDE发送通用通道消息”}”);
//这个也没用
//JSONObject JSONObject=新的JSONObject();
//put(“body”,“content\:\”欢迎使用java编码和http rest模板通过intellij IDE发送的常规频道消息\“}”);
//这个成功了
String requestJson=“{\'body\':{\'content\':\”欢迎使用java编码和http rest模板通过intellij IDE发送的常规频道消息\“}”;
//HttpEntity请求=新的HttpEntity(jsonObject.toString(),标头);
HttpEntity请求=新的HttpEntity(requestJson,headers);
ResponseEntity response=null;
试一试{
response=restTemplate.postForEntity(url、请求、字符串、类);
res=新的JSONObject(response.getBody());
系统输出打印项次(res);
}捕获(例外e){
e、 printStackTrace();
}
返回res;
}
您试过什么吗?如果是的话,你为什么不展示?若否,原因为何?