webservice学习笔记(十):用CXF编写基于spring的web service

1.编码实现

a.server端:

-创建spring的配置文件beans.xml,在其中配置SEI

·实体类,SEI及实现类代码如下:

·前面发布webservice是用endpoint,由于这一次是基于spring,所以我们使用的是xml配置文件,beans.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">

    <jaxws:endpoint id="criminalBegin" implementor="com.wayne.sei.impl.CriminalWsImpl"
        address="/gotham">
    </jaxws:endpoint>
</beans>

老版本的cxf框架,xml文件是需要添加三个import标签的,cxf.xml,cxf-extension-soap.xml,cxf-servlet.xml,

由于我们是新版本所以不需要,不然会报出异常,而我们的jaxws标签,id属性是唯一标识,implementor属性是全类名,

address属性是虚拟地址

-在web.xml中,配置上CXF的一些核心组件

web.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>day_fif_ws_cxf_spring_web_se</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置beans.xml文件 -->
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <!-- src下面文件参数为classpath -->
  <param-value>classpath:beans.xml</param-value>
  </context-param>
  <!-- 应用启动的监听器 -->
  <listener>
  <listener-class>
  org.springframework.web.context.ContextLoaderListener
  </listener-class>
  </listener>
  <!-- 所有的请求都会先经过cxf框架 -->
  <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>/*</url-pattern>
  </servlet-mapping>
</web-app>

·接下来我们把我们的java web项目部署到tomcat并开始运行,运行结果如图:

这里我们可以看到当加载beans.xml后就创建了我们的实现类对象

·访问url如图:

·访问wsdl文档路径如图:

我们之前在beans.xml里面配置了address属性,所以路径后面是项目名+address属性值+?wsdl

·使用eclipse的web service explorer验证我们的wsdl: 

b.client端

-生成客户端代码

·这里不再赘述直接上图:

-创建客户端的spring配置文件beans-client.xml,并配置

-编写测试类请求webservice:

猜你喜欢

转载自blog.csdn.net/weixin_40740613/article/details/83690733