WebService集成Spring

提供方使用spring

使用spring我们也一样需要添加依赖  我们把springmvc的包导进来就把spring的包全部添加进来了

 创建maven项目,在pom.xml文件中做出如下配置:

  

 <properties>  
    <version>2.7.18</version>  
 </properties>  
  
 <dependencies>  
  
     <dependency>  
<span style="white-space:pre">  </span><groupId>org.apache.cxf</groupId>  
    <artifactId>cxf-rt-frontend-jaxws</artifactId>  
    <version>${version}</version>  
</dependency>  
<dependency>  
    <groupId>org.apache.cxf</groupId>  
    <artifactId>cxf-rt-transports-http</artifactId>  
    <!-- 上面定义了一个<properties></properties> 版本号-->  
    <version>${version}</version>  
</dependency>  
<dependency>  
    <groupId>org.apache.cxf</groupId>  
    <artifactId>cxf-rt-transports-http-jetty</artifactId>  
    <version>${version}</version>  
</dependency>  
     
     <!-- 日志打印slf4j_2.12 -->  
<dependency>  
     <groupId>org.slf4j</groupId>  
     <artifactId>slf4j-log4j12</artifactId>  
     <version>1.7.2</version>  
</dependency>  
      
<!-- springmvc 依赖 -->  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-webmvc</artifactId>  
    <version>4.3.8.RELEASE</version>  
</dependency>  
 </dependencies>  

  之后写接口和实现类

  定义接口:

  

package cn.et.spring;  
  
import javax.jws.WebMethod;  
import javax.jws.WebService;  
  
/** 
 * 计算机接口 
 *  
 * */  
  
@WebService  
public interface FileService {  
  
    public abstract String getFile(String filePath);  
  
} 

    定义实现类:

  

package cn.et.spring;  
  
import java.io.File;  
  
import org.apache.cxf.helpers.FileUtils;  
  
/** 
 *计算机实现类  
 * */  
  
public class FileServiceImpl implements FileService {  
  
    @Override  
    public String getFile(String filePath) {  
          
        // cxf里面内置的一个对象 FileUtils 可以通过传入的路径直接返回你的文件内容。  
        return FileUtils.getStringFromFile(new File(filePath));  
    }  
      
} 

  在src目录下创建spring.xml文件

  

<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:jaxws="http://cxf.apache.org/jaxws"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="  
    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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
      
    <!-- 由spring容器将其创建bean -->  
    <context:component-scan base-package="cn"></context:component-scan>  
    <!-- 表示要使用 已经定义好的spring xml文件 初始化内部类 比如  JaxWsServerFactoryBean -->  
    <import resource="classpath:META-INF/cxf/cxf.xml" />  
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
      
      
      
    <!-- endpoint用于发布一个webservice  bean都要有id  
       http://tomcat发布时主机 ip:端口/项目上下文路径 / web配置的前缀/address的值?wsdl  
    -->  
    <jaxws:endpoint id="file" implementor="#fileServiceImpl"  
        address="/file" />     
</beans>

  最重要的是在maven项目下的web.xml里面需要配置spring的监听器和cxf的拦截器,配置如下:

  

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.5"   
    xmlns="http://java.sun.com/xml/ns/javaee"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
    <!-- 由web容器创建spring容器 -->  
    <context-param>  
      <param-name>contextConfigLocation</param-name>  
      <param-value>classpath:spring.xml</param-value>  
   </context-param>  
   
      
    <!-- 监听器 -->  
   <listener>  
      <listener-class>  
         org.springframework.web.context.ContextLoaderListener  
      </listener-class>  
   </listener>  
     
   <!-- 配置拦截器 拦截带有一个/services的  
    如果拦截所有的话我们访问jsp也会被拦截之后不会进入jsp页面会进入wsdl文件-->  
    <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>/services/*</url-pattern>  
    </servlet-mapping>  
      
  <welcome-file-list>  
    <welcome-file>index.jsp</welcome-file>  
  </welcome-file-list>  
</web-app>

之后可以通过地址访问以下wsdl文件   spring.xml文件中有提到路径的访问

  

调用方使用spring

首先添加依赖 同上一样的依赖

需要知道暴露的接口

定义接口:

  

package cn.et.spring;  
  
import javax.jws.WebService;  
  
  
@WebService  
public interface FileService {  
  
    public abstract String getFile(String filePath);  
  
}

定义spring.xml文件

  

<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:jaxws="http://cxf.apache.org/jaxws"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="  
    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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
      
    <!-- client:表示一个客服端     
        serviceClass:暴露的接口  
        address:wsdl地址   
     -->  
    <jaxws:client id="fileService" serviceClass="cn.et.spring.FileService" address="http://192.168.4.11:8080/CxfPub/services/file?wsdl"></jaxws:client>  
</beans> 

定义测试类:

  

package cn.et.spring;  
  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
public class TestSpringAndWebService {  
      
    public static void main(String[] args){  
          
        //创建spring容器  如果是web项目就是在web.xml文件中配置监听器  
        ClassPathXmlApplicationContext cxap=new ClassPathXmlApplicationContext("classpath:spring.xml");  
          
        //在配置文件中创建的bean  
        FileService fs=(FileService)cxap.getBean("fileService");  
          
        //传入一个文件进行测试  
        String str=fs.getFile("e:/a.txt");  
        System.out.println(str);  
          
    }  
  
} 
原文地址:https://www.cnblogs.com/xushirong/p/7181826.html