直接代码
服务器端
定义接口和实现类
IHelloWorld.java
package com.sf.module.lscmcommon.webservice;
import java.util.List;
import javax.jws.WebService;
@WebService
public interface IHelloWorld {
public String sayHello(String text);
public List<String> findObj();
}
HelloWorld.java
package com.sf.module.lscmcommon.webservice;
import java.util.List;
import javax.jws.WebService;
@WebService
public class HelloWorld implements IHelloWorld {
public String sayHello(String text) {
System.out.println("Hello" + text);
return "Hello" + text;
}
public List<String> findObj(){
return null;
}
}
配置Spring的xml文件
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws">
<!-- 这三个必加 -->
<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" />
<!-- cxf webService 接口实现类 -->
<bean id="hello" class="com.sf.module.lscmcommon.webservice.HelloWorld" />
<!--
id:指在spring配置的bean的ID.
Implementor:指明具体的实现类.
Address:指明这个web service的相对地址,如:http://10.0.7.115:8080/lscm/ws/IHelloWorld?wsdl
-->
<jaxws:endpoint id="helloWorld" implementor="#hello"
address="/IHelloWorld" />
</beans>
在web.xml里面
追加
... <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>/ws/*</url-pattern>
</servlet-mapping> ...
服务端完成,部署tomcat,打开地址.出现这玩意就是成功了.
客户端开发
新建一个工程,定义一个接口即可,和服务端的一致.
IHelloWorld .java
package com.cdg.webService;
import javax.jws.WebService;
@WebService
public interface IHelloWorld {
public String sayHello(String text);
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws">
<!--
id:不需要我解释
serviceClass:接口类包名
address:服务器的地址,后面的?wsdl不要
-->
<jaxws:client id="helloWorld"
serviceClass="com.cdg.webService.IHelloWorld"
address="http://10.0.7.115:8080/lscm/ws/IHelloWorld" />
<!-- 加载数据源 -->
<import resource="db-config.xml" />
</beans>
其他的都不用了;
编写个测试类看看有没有连接成功
package com.cdg.webService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloWorld {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:springConfig/applicationContext.xml");
IHelloWorld client = (IHelloWorld) ctx.getBean("helloWorld");
String result = client.sayHello("你好!");
System.out.println("回调 : " + result);
}
}
以上的包自己去下载.
温馨提示:如果有报JAXB的包的错误,就看是不是和jdk版本有冲突,换一下jaxb的版本即可.
看效果===================================================================