title: Accessing Restful webservice using restful Client in Spring 3
body: <p>I have created web service with restful in spring 3.0 and it's working good but I am unable to create restful client to access this web service. I searched lot on google for it but nothing useful. Here I created web service.
  Controller:</p>
  
  <pre><code>package com.nmmc.ws.controller;
  import java.util.List;
  import javax.inject.Inject;
  import javax.inject.Named;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.ResponseBody;
  import com.nmmc.ws.model.Corporator;
  import com.nmmc.ws.serviceImpl.CorporatorServiceImpl;
  
  @Controller
  public class WSDemoController
  {
      @Inject
      @Named("wsCorporator")
      private CorporatorServiceImpl corporatorServiceImpl;
  
      @RequestMapping(value = "/corporator.webService", method = RequestMethod.GET)
      @ResponseBody
      public List&lt;Corporator&gt; getAllCorporators() {
  
          List list = corporatorServiceImpl.getAllCorporators();
          System.out.println("list" + list);
          return list;
      }
  }
  </code></pre>
  
  <p>ServiceImpl:</p>
  
  <pre><code>package com.nmmc.ws.serviceImpl;
  import java.util.ArrayList;
  import java.util.List;
  import javax.inject.Inject;
  import javax.inject.Named;
  import com.nmmc.ws.daoImpl.CorporatorDAOImpl;
  import com.nmmc.ws.model.Corporator;
  @Named("wsCorporator")
  public class CorporatorServiceImpl
  {
      @Inject
      @Named("wsCorporatorDAO")
      private CorporatorDAOImpl corporatorDAOImpl;    
      public List&lt;Corporator&gt; getAllCorporators() {       
          List list = corporatorDAOImpl.getAllCorporators();      
          return list; 
      }
  }
  </code></pre>
  
  <p>DAOImpl:</p>
  
  <pre><code>package com.nmmc.ws.daoImpl;
  import java.util.ArrayList;
  import java.util.List;
  import javax.inject.Named;
  import com.nmmc.ws.model.Corporator;
  @Named("wsCorporatorDAO")
  public class CorporatorDAOImpl
  {   
      public List&lt;Corporator&gt; getAllCorporators() {           
          List list = new ArrayList();
          list.add("Some testing data...");
          return list; 
      }
  }
  </code></pre>
  
  <p>applicationContext.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:context="http://www.springframework.org/schema/context"
              xmlns:util="http://www.springframework.org/schema/util"
              xmlns:mvc="http://www.springframework.org/schema/mvc"
              xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
              http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
              http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"&gt;
  
              &lt;import resource="works-servlet.xml" /&gt;
  
          &lt;context:component-scan base-package="com.nmmc.ws"/&gt;
          &lt;!--&lt;context:component-scan base-package="com.nmmc.works.web" /&gt;     
          --&gt;&lt;bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"/&gt;  
          &lt;bean id="marshallingHttpMessageConverter"            class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"&gt;
              &lt;property name="marshaller" ref="xstreamMarshaller"/&gt;
              &lt;property name="unmarshaller" ref="xstreamMarshaller"/&gt;
          &lt;/bean&gt;
  
          &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;
              &lt;property name="messageConverters"&gt;
                  &lt;util:list id="beanList"&gt;
                      &lt;ref bean="marshallingHttpMessageConverter"/&gt;
                  &lt;/util:list&gt;
              &lt;/property&gt;
          &lt;/bean&gt;  
          &lt;mvc:annotation-driven/&gt;     
      &lt;/beans&gt;
  </code></pre>
  
  <p>I have created restfulClient in another module as given above just application.xml and controller as:
  application.xml:</p>
  
  <pre><code>&lt;bean id="restTemplate" class="org.springframework.web.client.RestTemplate"&gt;
          &lt;property name="messageConverters"&gt;
              &lt;list&gt;
                  &lt;!-- We only have one message converter for the RestTemplate, namely 
                      the XStream Marshller --&gt;
                  &lt;bean
                      class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"&gt;
                      &lt;constructor-arg&gt;
                          &lt;bean class="org.springframework.oxm.xstream.XStreamMarshaller"&gt;
                              &lt;!-- Explicitly define the aliases --&gt;
                              &lt;!-- &lt;property name="aliases"&gt; &lt;props&gt; &lt;prop key="article"&gt;com.informit.articleservice.model.Article&lt;/prop&gt; 
                                  &lt;prop key="category"&gt;com.informit.articleservice.model.Category&lt;/prop&gt; &lt;/props&gt; 
                                  &lt;/property&gt; --&gt;
  
                              &lt;!-- Tell XStream to find the alias names in the following classes --&gt;
                              &lt;property name="annotatedClasses"&gt;
                                  &lt;list&gt;
                                      &lt;value&gt;com.nmmc.ws.model.Corporator&lt;/value&gt;
                                  &lt;/list&gt;
                              &lt;/property&gt;
                          &lt;/bean&gt;
                      &lt;/constructor-arg&gt;
                  &lt;/bean&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
  
      &lt;mvc:annotation-driven /&gt;
  </code></pre>
  
  <p>ClientController:</p>
  
  <pre><code>package com.nmmc.ws.controller;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.springframework.stereotype.Component;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.client.RestTemplate;
  
  import com.nmmc.ws.model.Corporator;
  @Controller
  //@RequestMapping("/clientController")
  @Component( "articleClient" )
  public class WSClientController
  {
      protected RestTemplate restTemplate;
      private final static String articleServiceUrl = "http://myPortNo:8080/Works_Web/corporator.webService";
      @SuppressWarnings( "unchecked" )
      @RequestMapping(value = "/corporator.etenderingWS", method = RequestMethod.GET)
      public List&lt;Corporator&gt; getAllCorporators()
      {
          System.out.println("URI for request..." + articleServiceUrl);
          List list = new ArrayList();
          try{
              System.out.println("In try block...");
              Corporator result =  (Corporator) restTemplate.getForObject(articleServiceUrl, List.class); 
              list = (List) result;
  
          }catch(Exception e){
              e.printStackTrace();
          }
          return list;
      }   
  }
  </code></pre>
  
  <p>When I run application it gives error:</p>
  
  <pre><code>java.lang.NullPointerException
      at com.nmmc.ws.controller.WSClientController.getAllCorporators(WSClientController.java:49)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:616)
      at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)
      at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      at java.lang.Thread.run(Thread.java:636)
  </code></pre>