首先开发服务端:
一.在pom.xml 中加入依赖
<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>
然后在Maven dependencies看到引用的jar包
二. 服务端接口和实现类
首先接口类
package com.mycompany.webservice.server;
import javax.jws.WebService;
@WebService
public interface Greeting(){
public String greeting(String userName);
}
然后实现类
public class GreetingServiceImpl implement Greeting{
public String greeting(String userName){
return "Hello" + userName +",currentTime is"+Calendar.getInstance().getTime();
}
}
服务端代码完了
三 配置
在 application.xml中添加
<!--=============== 实现类的bean,需要spring注入 ============================-->
<bean id="greetingServiceImpl" class="com.mycompany.webservice.server.GreetingServiceImpl"/>
<jaxws:endpoint id="greetingService" implementor="#greetingServiceImpl" address="/Greeting" />
在web.xml中:
添加
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet- class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<!--==这个设置很重要,那么我们的webservice的地址就是http://localhost:8080/yourProgramName/webservice/Greeting===-->
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
服务端就开发好了。
就可以通过http://localhost:8080/yourProgramName/webservice/Greeting?wsdl 访问看到我们的wsdl了。
下面开发我们的客户端,来调用上面的服务。
package com.mycompany.webservice.client;
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”));
}
}
好了客户端也开发完成。