Spring Rest Template - Post Request String Body to azure gives 500 error

698 Views Asked by At

I'm able call 4-5 rest apis, prior to coming to this block of code. But I'm not able to execute this block of code.

I am using spring 4.0.4 core, mvc jars.

I am trying to call a azure ucwa web service using rest template, its a post method with string as body. I m getting a 500 internal error. I have searched all over internet and yet did not find solution, Please guide me. Thank you.

link : https://learn.microsoft.com/en-us/skype-sdk/ucwa/sendanim step 9

RestTemplate restTemplate = new RestTemplate();
   System.out.println(" jsonLinksObj : 4a "+jsonLinksObj);   
   org.json.JSONObject jsonSendMessageObj = jsonLinksObj.getJSONObject("sendMessage");
   String sendMsgFullUrl = poolUrl.concat(jsonSendMessageObj.getString("href")).concat("?OperationContext=4322131"); 

  headers.clear();
  headers.setContentType(MediaType.TEXT_PLAIN);
  headers.set("Authorization", "Bearer " + jwtToken);

  String body = "My Send Message Here";

                HttpEntity<Object> entityJsonSendMsg4 = new HttpEntity<Object>(body, headers);


                 ResponseEntity<Object> sbSendMsgObj  = null;
               try{

                    sbSendMsgObj = restTemplate.exchange(
                        new URI(sendMsgFullUrl), 
                        HttpMethod.POST, 
                        entityJsonSendMsg4, 
                        Object.class);
               } catch(Exception e){
                        e.printStackTrace();
               }

                            WARN : org.springframework.web.client.RestTemplate - POST request for "https://webpoolpnqin102.infra.lync.com/ucwa/oauth/v1/applications/102086376449/communication/conversations/46db8085-8ad0-4186-a4f0-9a521b256b9b/messaging/messages?OperationContext=4322131" resulted in 500 (Internal Server Error);         invoking error handler
                org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error
                                at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94)
                                                                                         at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:589)
                                at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:547)
                                at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:518)
                                at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:463)
                                at com.test.example.employee.test.EmployeeTest.sendMessage(EmployeeTest.java:266)
                                  at com.test.example.employee.test.EmployeeTest.main(EmployeeTest.java:337)
                Exception in thread "main" java.lang.NullPointerException
                                  at com.test.example.employee.test.EmployeeTest.sendMessage(EmployeeTest.java:331)
                                  at com.test.example.employee.test.EmployeeTest.main(EmployeeTest.java:337)
2

There are 2 best solutions below

0
curious_one On BEST ANSWER

I have clean and build the project. And run the code again. Its working fine, anybody want to send string in body, can use the above rest template code. Thank you.

0
Piotr Podraza On

It is hard to say what is the reason behind the 500 you got in response - you probably have to debug it yourself but it should not be very hard.

Since you created RestTemplate using default constructor you have got a deafault error handling. Run your application in debug mode and place a breakpoint in DefaultResponseErrorHandler::handleError. There you get the access to response represented by ClientHttpResponse class and can actually look into body you got back.

When you know the reason behind the error you can correct the request accordingly.