?
Solved

Trouble using Rest Template: org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error

Posted on 2013-02-04
4
Medium Priority
?
12,780 Views
Last Modified: 2013-02-06
We are having a problem calling a web service using Springs rest template when login and password info are included in the URL.  I'm not sure what is needed to use Springs Rest Template with this type of formatted web service URL call.

Here is a example mocked up URL:

https://api.companya.com/ws_20/basicquery.asmx/GetMembersChangedSince?login=7779D9A5-6937-46CC-91DD-5003FEB83B&password=KJCB349p8fyKJCNcx&columns=First_Name,id_number&changedColumns=&changedSince=2011-01-01&includeNonMembers=true&includeBlankIds=true&filter=[First_Name] like 'M%'

Open in new window


When I initialize the REST template and call the following:
String result = restTemplate.getForObject(url, String.class); 

Open in new window


I am getting the following error:
org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error

Open in new window


Below is a snippet of how I am initializing the Rest Template:

      
String url ="https://api.companya.com/ws_20/basicquery.asmx/GetMembersChangedSince?login=7779D9A5-6937-46CC-91DD-5003FEB83B&password=KJCB349p8fyKJCNcx&columns=First_Name,id_number&changedColumns=&changedSince=2011-01-01&includeNonMembers=true&includeBlankIds=true&filter=[First_Name] like 'M%'";
	this.restTemplate = getRestTemplate();	

    
      public RestTemplate getRestTemplate() {
        
        ContextAwareHttpComponentsClientHttpRequestFactory customRequestFactory = getRequestFactory();
        RestTemplate restTemplate = new RestTemplate(customRequestFactory);
        return restTemplate;

      }


      private ContextAwareHttpComponentsClientHttpRequestFactory getRequestFactory() {
          
          HttpHost targetHost = new HttpHost(url);
          
          DefaultHttpClient httpclient = new DefaultHttpClient();
          HttpClient hclient = new DefaultHttpClient();
          
          hclient = HttpsClientService.wrapClient(hclient);
          httpclient = (DefaultHttpClient)hclient;
      
               
      ContextAwareHttpComponentsClientHttpRequestFactory customFactory = new ContextAwareHttpComponentsClientHttpRequestFactory( httpclient);
      return customFactory;

      }

Open in new window


Any suggestions would be appreciated.

Thanks
0
Comment
Question by:West100
  • 3
4 Comments
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 38853788
org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error
This is what the web service has returned to you. It is saying that the Web Services server has encountered the error. So it is a problem specific to the web service that you are calling.

If you can 'catch' this exception in your code, you could interrogate the HttpServerErrorException object, calling it's various 'get...' methods to see if there is any more useful information in the response body or headers, etc.

Otherwise, do you have the web service call working with something else? ie. using SoapUI or maybe a .NET client, etc. If you have a working version, then you could use WireShark, etc to trace the actual request/response traffic and compare between the working version and this error version, and see where the differences are.

Or if none of this helps, talk to the people responsible for the web service and get them to investigate. As the error says, it is an "Internal" error and so even if it is due to something in your URL, etc. they really should be handling this properly. The 500 Internal Server Error is the catch-all error, that if the server code throw an Exception that is handled in anyway in their code, that is what is sent back to the client.
0
 
LVL 36

Expert Comment

by:mccarl
ID: 38853798
And on an aside, you should be able to get rid of lines 18 and 22, from that snippet that you posted, and just pass 'hClient' directly to the constructor of the RequestFactory, as it should be accepting a HttpClient object not the more specific DefaultHttpClient.

At the very least, remove the call to 'new DefaultHttpClient()' on line 18 as that new object that is created is never used!

Oh, and it is clear from just the small part of the code that you posted, but if these methods are something that is called reasonably often, you should consider 'caching' these objects, so that you don't have to construct them everytime they are retrieved. Maybe you have already done this elsewhere, but I just thought I would mention it.
0
 

Author Closing Comment

by:West100
ID: 38860075
By digging into the HttpServerErrorException as you suggested we were able to see that one of the param's being passed had to be populated in a very specific format.  Still more of a problem for the web service provider to resolve.  They should not be returning a 500.

Thanks
0
 
LVL 36

Expert Comment

by:mccarl
ID: 38862082
one of the param's being passed had to be populated in a very specific format
This may be what you are referring to, but I just had a look at your URL, and you should probably "URL Encode" it (it being the whole query string, not just one or two parameters) before sending your requests.

Glad the details in HttpServerErrorException helped you though! :)
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
No other job is as rewarding and demanding as building an iPhone app is. It is not really in the hands of the developer for the success of an iPhone app. Many factors operate jointly for every iOS application's success in the market.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question