我在Spring 3.0中创建了一个非常安静的Web服务,它工作正常,但是我无法创建安静的客户端来访问这个Web服务。我在谷歌搜索了很多但没有用。在这里,我创建了Web服务。 控制器:
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>
我已经在另一个模块中创建了restfulClient,如上面给出的application.xml和controller一样: 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;
}
}
当我运行应用程序时,它会出错:
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)
答案 0 :(得分:0)
我想您可能忘记了客户端控制器中RestTemplate bean的依赖注入注释。
答案 1 :(得分:-1)
您忘记初始化RestTemplate。 RestTemplate t = new RestTemplate();
Gook Luck ......