服务端(SSM)项目的配置:
1.在已经写好的SSM项目中配置WebService的依赖:
<cxf.version>3.0.1</cxf.version>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
2.定义需要用到SSM项目中那几个功能的接口和实现类(这个不是完整的maven项目,只有一个pom文件,所有包都在这个文件夹下)
3.在接口和实现类中写自己需要调用的方法(需要注意的是在接口类的上方一定要写@WebService注解)
4.实现自己写的接口,按照正常的实现方法就行了(给大家随便看一个例子,平时怎么写SSM的实现类,这里就怎么写就行了)
6.配置spring-cxf.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!--
id:随便起
class:为接口的实现类的位置
-->
<bean id="shopWs" class="cn.yong.ws.impl.ShopWSImpl"></bean>
<!--
id:随便起
address:访问的WebService的name
访问的时候:(http://ip地址/service/shops?wsdl) service会在web.xml中解释
我的是http://localhost:8081/service/shops?wsdl
bean:就是上面bean中定义的id
-->
<jaxws:server id="shopWebservice" address="/shops">
<jaxws:serviceBean><ref bean="shopWs" /></jaxws:serviceBean>
</jaxws:server>
</beans>
7.接下来是web.xml文件中的配置(注意:web.xml文件需要可以读到创建的spring-cxf文件):
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/service/*</url-pattern> <!-- 因为这里是service,所以访问的时候需要加service -->
</servlet-mapping>
配置好了之后在浏览器中输入spring-cxf.xml中注释的内容可以在浏览器中看到以下内容:
接下来是客户端的设置:
1.首先在运行PC的cmd界面,在界面创建aaa(随便起)文件夹,并进入当前文件夹:
2.在aaa>后面输入 wsimport -s . http:// <http://为刚才在浏览器中输入的所有内容>
进入aaa文件夹下把生成的文件考到客户端(SSH项目中),需要考的是java文件和impl中的(项目下的webservice文件夹下)
3.定义spring-cxf.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!--
id随便起
serviceClass是赋值粘贴到的那个文件的路径
shops为ssm项目中自己定义的address
address就是下面的:http://ip地址/service/shops
-->
<jaxws:client id="customerWebservice" serviceClass="cn.lichenyang.dms.webService.IShopWS"
address="http://localhost:8081/service/shops"></jaxws:client>
</beans>
4.在web.xml文件中配置ssm中配置的东西(需要注意位置)
5.接下来通过action调用就行了