CXF整合 ssm(重点CXF实现WebService以及客户端动态调用)

1.先将ssm框架搭好

这是整个服务端SSM+cxf

2.添加CXF的Maven

<dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.1.6</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-core -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-core</artifactId>
        <version>3.1.6</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>3.1.6</version>
    </dependency>

这是cxf的maven ssm我就不贴了网上很多!

3.使用注解发布接口

这是要发布的接口

4.配置实现类

接口的实现类

## 5去写cxf 的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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
     <import resource="classpath:META-INF/cxf/cxf.xml" /> 
   <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 
     <!-- 自动扫描webService -->
    <context:component-scan base-package="com.jksd" />
    <!-- implementor这里的是实现类的地址-->
    <jaxws:endpoint   implementor="com.jksd.service.servicesImpi.ServicesImpi" address="/HelloWorld"/>

</beans>

6.配置web.xml

添加上

```java
<!-- Bootstraps the root web application context before servlet initialization -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<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>/webservice/*</url-pattern>
    </servlet-mapping>

7.在appcontext.xml中引入spring-cxf.xml

 <import resource="classpath:spring-cxf.xml" />

然后启动项目去访问
http://localhost:8081/ssm_2/webservice/HelloWorld?wsdl
说明发布成功了

8.创建客户端

将Maven 上面的依赖倒进去
客户端的简单结构

9.运行测试类

测试类
这里是动态调用其他的调用方式测试过没问题就不演示了!

!实体类的包名要和服务端一直 否则会报对象转换失败!下面演示下这个错误
这是实体类的包路劲不对将路径改正常后测试:
成功查询到数据

猜你喜欢

转载自blog.csdn.net/weixin_44695125/article/details/103969131