pom依赖
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.8</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.8</version>
</dependency>
webservice服务端
spring-cxf.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:jaxws="http://cxf.apache.org/jaxws"
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-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:endpoint id="helloService" implementor="com.test.cxf.HelloWorldServiceImpl" address="/test"/>
</beans>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-cxf.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
</web-app>
定义webservice接口及实现
@WebService
public interface HelloWorldService {
String sayHello();
}
@WebService
public class HelloWorldServiceImpl implements HelloWorldService {
@Override
public String sayHello() {
System.out.println("进入了远端服务器");
return "haha";
}
}
启动服务器就行了-----访问http://localhost:8080/项目名/service/test?wsdl
webservice客户端(第一种:使用注解)
spring-cxf.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:jaxws="http://cxf.apache.org/jaxws"
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-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:client id="helloService" serviceClass="com.test.IHelloService" address="http://localhost:8080/cxf-server/service/test?wsdl"></jaxws:client>
</beans>
自定义与服务端相同接口下的方法
@WebService(targetNamespace="http://cxf.test.com/",name="HelloWorldService")
public interface IHelloService {
public String sayHello();
}
测试
@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-cxf.xml");
IHelloService service = (IHelloService)context.getBean("helloService");
System.out.println(service.sayHello());
}
webservice客户端(第二种:命令行引入Java代码)
打开cmd,输入命令行
wsimport -s 本地路径 -p 包名 -keep wsdl路径名
解析成功后看到如下目录
![]()
测试
@Test
public void test1() {
HelloWorldService he = new HelloWorldServiceImplService().getHelloWorldServiceImplPort();
he.sayHello();
}
大功告成!