. 新建web project 并加入apachecxf\lib所有包编写要发布的web service 接口和实现这一步与前面一样
import javaxjwsWebService;
@WebService
public interface HelloWorld {
public String sayHello(String text);
}
import javaxjwsWebService;
@WebService(endpointInterface=testHelloWorld)
public class HelloWorldImpl implements HelloWorld {
public String sayHello(String text) {
return Hello + text ;
}
}
@WebService 注解表示是要发布的web 服务
在springcxfxml配置发布的web service
<?xml version= encoding=UTF?>
<beans xmlns=
xmlns:xsi=instance
xmlns:jaxws=
xsi:schemaLocation=
beansxsd
>
<import resource=classpath:METAINF/cxf/cxfxml />
<import resource=classpath:METAINF/cxf/cxfextensionsoapxml />
<import resource=classpath:METAINF/cxf/cxfservletxml />
<bean id=hello class=testHelloWorldImpl />
<jaxws:endpoint id=helloWorld implementor=#hello
address=/HelloWorld />
</beans>
注意<jaxws:endpoint id=helloWorld implementor=#hello
address=/HelloWorld />
id指在spring配置的bean的ID
Implementor:指明具体的实现类
Address:指明这个web service的相对地址
. 配置webxml文件
<?xml version= encoding=UTF?>
<webapp version= xmlns=
xmlns:xsi=instance
xsi:schemaLocation=
app__xsd>
<contextparam>
<paramname>contextConfigLocation</paramname>
<paramvalue>classpath:springcxfxml</paramvalue>
</contextparam>
<listener>
<listenerclass>
orgsprntextContextLoaderListener
</listenerclass>
</listener>
<servlet>
<servletname>CXFServlet</servletname>
<servletclass>
orgapachecxftransportservletCXFServlet
</servletclass>
<loadonstartup></loadonstartup>
</servlet>
<servletmapping>
<servletname>CXFServlet</servletname>
<urlpattern>/*</urlpattern>
</servletmapping>
</webapp>
.部署到tomcat服务器输入//localhost:/<webappname>/ HelloWorld?wsdl将显示这个web service的wsdl
注意如果webxml配置<servletname>CXFServlet</servletname>
<urlpattern>/ws/*</urlpattern>
则访问地址为/ws///localhost:/<webappname>/ws/ HelloWorld?wsdl
下面就开始创建一个客户端程序访问这个web service 同样新建java project 并加入apachecxf\lib所有包 创建与具体webservice技术无关的业务接口HelloWorldjava
public interface HelloWorld {
public String sayHello(String text);
}
配置springclientxml
<beans xmlns=
xmlns:xsi=instance
xmlns:jaxws=
xsi:schemaLocation=
beansxsd
>
<bean id=client class=testHelloWorld
factorybean=clientFactory factorymethod=create/>
<bean id=clientFactory class=orgapachecxfjaxwsJaxWsProxyFactoryBean>
<property name=serviceClass value=testHelloWorld/>
<property name=address value=//localhost:/helloWorld/>
<! 这个地方的地址一定要注意正确的>
</bean>
</beans>
.测试
import orgntextApplicationContext;
import orgntextsupportClassPathXmlApplicationContext;
import testHelloWorld;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
springclientxml);
HelloWorld client = (HelloWorld) ctxgetBean(client);
String result = clientsayHello(你好!);
Systemoutprintln(result);
}
}