cxf就不多介绍了 ,直接上代码例子了:
1:接口类
package service;
import javax.jws.WebService;
/**
* WebService接口
* */
@WebService
public interface IHelloService {
public String sayHello( String username);
}
2:接口的实现类:
package service.impl;
import javax.jws.WebMethod;
import javax.jws.WebService;
import service.IHelloService;
@WebService(serviceName="HelloService",endpointInterface="service.IHelloService")
public class HelloServiceImpl implements IHelloService {
@Override
public String sayHello(String username) {
// TODO Auto-generated method stub
String name="hello"+username;
return name;
}
}
3:创建src/applicationContext-server.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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 手动的添加头信息:
xmlns:jaxws="http://cxf.apache.org/jaxws"
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"
-->
<!--在META-INF创建文件夹cxf ,并在cxf里面建立 cxf.xml cxf-extension-soap.xml cxf-servlet.xml (例如:META-INF/cxf/cxf.xml)
文件里面的内容可以去 cxf-2.7.1.jar/META-INF/cxf/cxf.xml
cxf-extension-soap.xml cxf-servlet.xml 里面复制对应的内容-->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="helloServiceImpl" class="service.impl.HelloServiceImpl" />
<!-- 注意下面的address,这里的address的名称就是访问的WebService的name 访问路径 :项目路径 + address+?wsdl(http://127.0.0.1:8080/webService/helloService?wsdl) -->
<jaxws:server id="helloService" serviceClass="service.IHelloService"
address="/helloService">
<jaxws:serviceBean>
<!-- 要暴露的 bean 的引用 -->
<ref bean="helloServiceImpl" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
4:建立src/applicationContext-client.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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!--/webService/
***注意***
手动添加的内容:
xmlns:jaxws="http://cxf.apache.org/jaxws"
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
-->
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxws:client id="userWsClient" serviceClass="service.IHelloService"
address="http://127.0.0.1:8080/webService/helloService"/>
</beans>
5:web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- 加载Spring容器配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-server.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
6:测试类:
package service.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.IHelloService;
public class TestSpringCxf {
@Test
public void testSpringCxf(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");
IHelloService service = ctx.getBean("userWsClient", IHelloService.class);
String name = service.sayHello("小辉");
System.out.println("#############Client getUserByName##############"+name);
}
}
第一篇博文不好请见谅