WebService的部署搭建
服务端搭建:
1、CXF和spring整合需要导入相关jar包文件.
这里只写明CXF的相关jar包:
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-rs-extension-providers -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.1.4</version>
</dependency>
2、在web.xml文件中添加:
<servlet>
<description>Service</description>
<servlet-name>Service</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Service</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
3、创建cxf-server.xml文件,配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<!-- webServer 服务的业务实现类 -->
<bean id="leiService" class="com.lei.interfaces.service.impl.LeiServiceImpl">
</bean>
<!--业务接口 注意下面的address,这里的address的名称就是访问的WebService的name -->
<jaxws:server id="iLeiService" serviceClass="com.lei.interfaces.service.ILeiService" address="/lei">
<jaxws:serviceBean>
<!-- 要暴露的 bean 的引用 -->
<ref bean="leiService"></ref>
</jaxws:serviceBean>
</jaxws:server>
</beans>
注意事项:
(1)、当CXF的jar包版本不低于3.0,在配置文件中不需要添加:
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
4、创建服务器端接口以及实现类,代码如下:
@WebService
public interface ILeiService {
public String getString();
public LeiVO getString(String name,Page page);
}
@WebService
public class LeiServiceImpl implements ILeiService {
@Override
public String getString(){
return "hello!";
}
@Override
public LeiVO getString(String name,Page page){
LeiVO leiVO = new LeiVO();
//代码省略.......
leiVO.setList(...);//集合类型用具体实现类,如:arrayList hashMap等
leiVO.setPage(page)
return leiVO;
}
}
当需要给客户端返回多个对象时,重新写个类里面包含多个对象,如下:
public class LeiVO implements Serializable{
private ArrayList<> list;
private Page page;
//省略........
}
6、启动tomcat服务器后,在WebBrowser中请求:http://localhost:8080/services/lei?wsdl 如果你能看到wsdl的xml文件的内容,就说明你成功了
客户端搭建
1、导入CXF的jar包—-和服务端一样。
2、创建cxf-client.xml文件,配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> -->
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<jaxws:client id="iLeiService"
serviceClass="com.lei.interfaces.service.ILeiService" address="http://192.168.1.201:8070/services/lei">
</jaxws:client>
</beans>
和服务端的第3项注意事项一样
4、创建客户端接口、实体类,必须与服务端一致
@WebService
public interface ILeiService {
public String getString();
public LeiVO getString(String name,Page page);
}