**
使用CXF创建WEB SERVICE
**
最近接手一个项目,项目需要创建一个网站,并能够与其他网站进行数据交换,首先想到里WEB SERVICE ,创建web service的方法有多种,网上都有,主要着这里写一下使用CXF创建web service
首先引入包:
将jar放到项目的lib文件夹下
修改web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-server.xml
</param-value>
</context-param>
<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>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
创建服务端xml:
applicationContext-server.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!--
***注意***
手动添加的内容:
xmlns:jaxws="http://cxf.apache.org/jaxws"
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
-->
<!-- CXF 引入必要的xml,在jar包里已经有了 -->
<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:endpoint id="helloService" implementor="web service接口实现类" address="/web service地址" />
</beans>
创建接口
import java.util.List;
import javax.jws.WebService;
/**
* @author 作者 :sunguoqiang
* 修改人:
* 修改日期:
* 类说明:
*/
@WebService
public interface testService {
public String hello(@WebParam(name = "arg0") String text);
}
创建接口实现类
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author 作者 :sunguoqiang
* 修改人:
* 修改日期:
* 类说明:
*/
@WebService(endpointInterface = "接口地址", serviceName = "HelloService")
public class testServiceImpl implements testService {
@Override
public String hello(String text) {
String hello = "hello:"+text;
return hello;
}
}
配置成功
输入http://localhost:8080/“你的项目名称”/ws/web service地址?wsdl
创建服务端,如果在同一个项目则只需要创建spring-client.xml和一个客户端就行
spring-client.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schema/jaxws.xsd">
<bean id="client" class="com.dykj.GSM.service.emergencyscheduling.testServuce" factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<!-- 创建service -->
<property name="serviceClass" value="服务端接口路径" />
<!-- 接口地址 -->
<property name="address" value="服务端接口地址" />
</bean>
</beans>
客户端代码:
/**
* @author 作者 :sunguoqiang
* 修改人:
* 修改日期:
* 类说明:
*/
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class web {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-client.xml");
testServuce client = (testService) ctx.getBean("client");
String result = client.hello("word");
System.out.println(result);
}
}
如果不是在同一个项目,需要在新的项目中引入之前的jar,除了创建之前的xml和客户端之外还需要创建一个service:
testServIce:
import java.util.List;
import javax.jws.WebService;
/**
* @author 作者 :sunguoqiang
* 修改人:
* 修改日期:
* 类说明:
*/
@WebService(targetNamespace="wsdl下<wsdl:import>里的namespace,或者将客户端的包路径设置成服务端一样")
public interface testService {
public String hello();
}