Solved

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

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Read about why website design really matters in today's demanding market.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn how to implement Singleton Design Pattern in Java.

757 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now