项目需要使用springmvc发布一个对外的服务,原来使用spring+cxf的结合,使用axis2的客户端调用,没有任何问题,但是使用pb9的客户端调用,一直出现一个莫名奇妙的错误(PNBI),好几天没有解决,也有说PB9调用CXF的服务有BUG,不知道确切的问题。
原来对axis2的框架比较熟悉,所以,把项目改成spring+axis2的服务。过程如下:
1、建立一个web project,导入spring的支持jar包。
2、导入axis2的支持jar包。
3、配置web.xml的文件
<!-- spring核心配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-*.xml</param-value> </context-param>
<!-- axis --> <servlet> <display-name>Apache-Axis Servlet</display-name> <servlet-name>AxisServlet</servlet-name> <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>AxisAdminServlet</servlet-name> <servlet-class>org.apache.axis2.webapp.AxisAdminServlet </servlet-class> </servlet>
<servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>*.jws</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services(此处services名称一定不能变,不然会有莫名奇妙的错误,切记切记,在此耽误了一天时间)/*</url-pattern> </servlet-mapping>
4、配置service.xml文件,位置在/WEB-INF/services/mydomain(此处名称任意)/META-INF/services.xml
<?xml version="1.0" encoding="UTF-8"?> <serviceGroup> <service name="personService" targetNamespace="http://impl.service.as.com"> <description>用户服务</description>
<!--SpringBeanName不能变,personService是定义的ID,你懂的--> <parameter name="SpringBeanName">personService</parameter> <parameter name="ServiceObjectSupplier"> org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier </parameter> <parameter name="ServiceClass"> com.as.service.impl.PersonServiceImp </parameter> <messageReceivers> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> </messageReceivers> </service> </serviceGroup>
5、spring的application.xml中配置如下
<bean id="applicationContext" class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" /> <bean id="personService" class="com.as.service.impl.PersonServiceImp"/>
6、定义一个接口和实现接口的类
接口
package com.as.service;
import com.as.domain.Person;
public interface PersonService { /*public Person getPersonById(String id);*/ public String helloWorld(String name); }
实现类
package com.as.service.impl;
import javax.annotation.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.transaction.annotation.Transactional;
import com.as.domain.Person; import com.as.service.PersonService;
@Transactional public class PersonServiceImp implements PersonService {
@Override public String helloWorld(String name) { // TODO Auto-generated method stub return "hello,"+name; } }
然后,发布到tomcat中,访问地址:
http://localhost:8080/AxisSpring/servic
总结,一个问题折磨了我一天时间,就是第三步提到的services的路径问题,我沿用了原来项目的webservice的定义,导致调用的时候,提示
org.apache.axis2.AxisFault: The service cannot be found for the endpoint reference (EPR)
怎么也检查不出问题所在,无意中看到这个路径,修改以后就正常了,不知道什么原因。
axis2客户端调用代码
public static void main(String[] args) { PersonServiceStub pss = null; try { pss = new PersonServiceStub("http://localhost:8080/AxisSpring/services/personService?wsdl"); } catch (AxisFault e) { // TODO Auto-generated catch block e.printStackTrace(); } HelloWorld hw = new HelloWorld(); hw.setName("unaras");
HelloWorldResponse hwr=null; try { hwr = pss.helloWorld(hw); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(hwr.get_return()); }
成功。
pb9的调用代码 soapconnection conn_soap personservicehttpsoap11endpoint smsport long rVal String endpoint="http://localhost:8080/AxisSpring/services/personService" conn_soap = CREATE soapconnection smsport = CREATE personservicehttpsoap11endpoint
rVal = conn_soap.CreateInstance(smsport,"personservicehttpsoap11endpoint",endpoint) conn_soap.SetOptions("SoapLog=~"mySoapLog.log~"") //conn_soap.SetOptions() if rVal <> 0 then messagebox('提示:','连接网络URL 出现错误,错误代码: '+string(rval)) return end if String rs
rs = smsport.helloWorld("unaras") messagebox('标题',rs)
destroy conn_soap destroy smsport