Solved

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

Posted on 2013-02-04
4
7,742 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 35

Accepted Solution

by:
mccarl earned 500 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 35

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 35

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

773 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