开发之前需要应用需要包
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-api</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-bindings-soap</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-security</artifactId>
<version>2.5.0</version>
</dependency>
一、服务端
1、服务的接口和实现
import javax.jws.WebService;
@WebService
public interface Greeting {
public String greeting(String userName);
}
import java.util.Calendar;
import javax.jws.WebService;
@WebService(endpointInterface = "com.mycompany.webservice.server.Greeting")
public class GreetingServiceImpl implements Greeting {
public String greeting(String userName) {
return "Hello " + userName + ", currentTime is "
+ Calendar.getInstance().getTime();
}
}
二、客户端
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class GreetingServiceClient {
public static void main(String[] args) {
//创建WebService客户端代理工厂
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//注册WebService接口
factory.setServiceClass(Greeting.class);
//设置WebService地址
factory.setAddress("http://localhost:8080/yourProgramName/webservice/Greeting");
Greeting greetingService = (Greeting)factory.create();
System.out.println("invoke webservice...");
System.out.println("message context is:"+greetingService.greeting("gary"));
}
}