//maven pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liyi.lianxi</groupId>
<artifactId>hellocxf1</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>hellocxf1 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- CXF Dependencies -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- Spring Dependencies ${spring.version} -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- 跳过测试单元 -->
<skip>true</skip>
</configuration>
</plugin>
</plugins>
<finalName>hellocxf1</finalName>
</build>
<properties>
<junit.version>4.11</junit.version>
<cxf.version>2.2.3</cxf.version>
<spring.version>3.2.3.RELEASE</spring.version>
<slf4j.version>1.7.7</slf4j.version>
</properties>
</project>
//spring 核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<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" />
<!-- 要暴露给外部调用的接口,address:请求路径 -->
<jaxws:endpoint implementor="com.liyi.webservice.HelloCxfImpl" address="/HelloCxf">
</jaxws:endpoint>
</beans>
//接口
package com.liyi.webservice;
import javax.jws.WebService;
@WebService
public interface HelloCxf {
public void sayHello();
}
//实现类
package com.liyi.webservice;
import javax.jws.WebService;
@WebService(endpointInterface = "com.liyi.webservice.HelloCxf")
public class HelloCxfImpl implements HelloCxf{
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("helloCxf");
}
}
//启动项目 访问 如访问成功,则服务器端代码编写完毕
//配置测试xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<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" />
<!-- serviceClass:要访问的接口,address:接口地址 这里不要配置?wsdl不然会报错-->
<jaxws:client id="helloCxfClient" serviceClass="com.liyi.webservice.HelloCxf"
address="http://127.0.0.1:8080/hellocxf1/services/HelloCxf" />
</beans>
//把服务器端接口拷贝过来
package com.liyi.webservice;
import javax.jws.WebService;
@WebService
public interface HelloCxf {
public void sayHello();
}
//编写测试代码
package com.liyi.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.liyi.webservice.HelloCxf;
public class TestCxf {
public static void main(String[] args) {
//加载Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");
HelloCxf helloCxf= context.getBean("helloCxfClient",HelloCxf.class);
helloCxf.sayHello();
}
}
//run一下 就可以调到服务器端的方法
转载于:https://my.oschina.net/u/1998885/blog/467212