Solved

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

Posted on 2013-02-04
4
9,342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 36

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 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

688 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