Issue
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:
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<Corporator> getAllCorporators() {
List list = corporatorServiceImpl.getAllCorporators();
System.out.println("list" + list);
return list;
}
}
ServiceImpl:
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<Corporator> getAllCorporators() {
List list = corporatorDAOImpl.getAllCorporators();
return list;
}
}
DAOImpl:
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<Corporator> getAllCorporators() {
List list = new ArrayList();
list.add("Some testing data...");
return list;
}
}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<import resource="works-servlet.xml" />
<context:component-scan base-package="com.nmmc.ws"/>
<!--<context:component-scan base-package="com.nmmc.works.web" />
--><bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"/>
<bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="xstreamMarshaller"/>
<property name="unmarshaller" ref="xstreamMarshaller"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list id="beanList">
<ref bean="marshallingHttpMessageConverter"/>
</util:list>
</property>
</bean>
<mvc:annotation-driven/>
</beans>
I have created restfulClient in another module as given above just application.xml and controller as: application.xml:
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<!-- We only have one message converter for the RestTemplate, namely
the XStream Marshller -->
<bean
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg>
<bean class="org.springframework.oxm.xstream.XStreamMarshaller">
<!-- Explicitly define the aliases -->
<!-- <property name="aliases"> <props> <prop key="article">com.informit.articleservice.model.Article</prop>
<prop key="category">com.informit.articleservice.model.Category</prop> </props>
</property> -->
<!-- Tell XStream to find the alias names in the following classes -->
<property name="annotatedClasses">
<list>
<value>com.nmmc.ws.model.Corporator</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
</list>
</property>
</bean>
<mvc:annotation-driven />
ClientController:
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<Corporator> 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;
}
}
When I run application it gives error:
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)
Solution
I think maybe you forgot the dependency injection annotation for your RestTemplate bean in your client controller.
Answered By - sdouglass