1、引入相关jar,所需jar如下
- 第一步,新建webservice接口
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC)
public interface TestService {
}
- 接口实现类
package com.vsc.webservice.impl;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import com.vsc.webservice.TestService;
@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC)
public class TestServiceImpl implements TestService {
}
- 服务端配置,cxfServer.xml
《?xml version="1.0" encoding="UTF-8"?》
《beans xmlns="http://www.springframework.org/schema/beans"
《/beans》
-
web.xml配置
《web-app version="2.5"
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_2_5.xsd">
《context-param>
《param-name>contextConfigLocation《/param-name>
《param-value>classpath:cxfServer.xml《/param-value>
《/context-param>
《!-- 加载spring容器的配置文件 -->
《listener>
《listener-class>org.springframework.web.context.ContextLoaderListener《/listener-class>
《/listener>
《!-- 处理由JavaBeans Introspector的使用而引起的缓冲泄露 -->
《listener>
《listener-class>org.springframework.web.util.IntrospectorCleanupListe ner《/listener-class>
《/listener>
《!-- cxf 配置 -->
《servlet>
《servlet-name>CXFServlet《/servlet-name>
《servlet-class>org.apache.cxf.transport.servlet.CXFServlet《/servlet-class>
《/servlet>
《servlet-mapping>
《servlet-name>CXFServlet《/servlet-name>
《url-pattern> /* 《url-pattern>
// int aa= testService.testHello2();
// System.out.println("aa="+aa);
// testService.testHello4("123");
} }
-
- 启动项目,访问http://localhost:8080/testWebService/test?wsdl,如果可以访问,则服务端发布成功。
-
3、客户端开发,客户端与服务端分离,新建web项目,SceneMan,引入上述jar
- 第一步,新建与服务端相同的接口,接口包路劲与服务端相同
package com.vsc.webservice;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC)
public interface TestService {//无返回值
public void testHello();
//返回string
public String testHello1();
//返回int
public int testHello2();
//接收参数 ,返回
public void testHello4(@WebParam(name="name") String name);
}- 第一种调用,测试
public class TestWebService {
public static void main(String args[]){
JaxWsProxyFactoryBean factory=new JaxWsProxyFactoryBean();
factory.setServiceClass(TestService.class);
factory.setAddress("http://localhost:8080/testWebService/test");
TestService testService= (TestService) factory.create();
testService.testHello1();
// int aa= testService.testHello2();
// System.out.println("aa="+aa);
// testService.testHello4("123");
} }
- 第二种调用测试,使用配置,cxfClient.xml
《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://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 《!-- 引入cxf配置的3个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"/>
《jaxws:client id="serviceClient" serviceClass="com.vsc.webservice.TestService"
address="http://localhost:8080/testWebService/test">《/jaxws:client>
《/beans>- 测试
public class TestWebService {
public static void main(String args[]){
ApplicationContext application=new ClassPathXmlApplicationC ontext("cxfClient.xml");
TestService testService=(TestService) application.getBean("serviceClient");
int aa= testService.testHello2();
System.out.println("aa="+aa);
} }