Spring maven 结合cxf 开发webservice

一  maven 依赖

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.joinus</groupId>
  <artifactId>ExcelPU</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>ExcelPU Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <org.springframework.version>4.3.7.RELEASE</org.springframework.version>
    <cxf.version>3.3.3</cxf.version>


  </properties>

  <dependencies>


    <!-- spring core -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>


    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1</version>
    </dependency>

    <dependency>
      <groupId>javax.xml</groupId>
      <artifactId>jaxb-api</artifactId>
      <version>2.1</version>
      <type>pom</type>
    </dependency>

    <dependency>
      <groupId>javax.xml</groupId>
      <artifactId>jaxb-impl</artifactId>
      <version>2.1</version>
    </dependency>

    <dependency>
      <groupId>xfire</groupId>
      <artifactId>saaj-api</artifactId>
      <version>1.3</version>
    </dependency>

    <dependency>
      <groupId>xfire</groupId>
      <artifactId>saaj-impl</artifactId>
      <version>1.3</version>
    </dependency>

    <dependency>
      <groupId>wsdl4j</groupId>
      <artifactId>wsdl4j</artifactId>
      <version>1.6.2</version>
    </dependency>

    <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>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http-jetty</artifactId>
      <version>${cxf.version}</version>
    </dependency>




    <!--poi附件上传包-->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>4.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>4.0.1</version>
    </dependency>

  </dependencies>

  <build>
    <finalName>ExcelPU</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

二  spring 依赖

<?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">

   <!--请注意,此处的class就是指接口服务端所在的类的具体路径,id可以自行设置,但是必须唯一-->
    <bean id="ExcelUtil"  class="com.joinus.app.ExcelUtil"></bean>
   <!--implementor: 这里为所需要实现接口服务端的类bean的id  必须要带#号  address:服务发布地址 id随便设置,但是不可以重复 -->
    <jaxws:endpoint id="ExcelUtilWB" implementor="#ExcelUtil"
                    address="/ExcelUtilWB"/>


</beans>

三  web.xml 配置

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!--配置log4j配置文件-->
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
  </context-param>
 <!--配置spring-cxf配置文件-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-cxf.xml</param-value>
  </context-param>

    


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
<!--  这边配置与servlet配置同理,url中的配置为访问地址,也可以配置为/*  但是为了防止在调用时与其他框架的调用地址混淆,建议前方加入一层名称 比如/ws/*-->
    <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>/ws/*</url-pattern>
    </servlet-mapping>



</web-app>

四  文件目录

五  具体实现 

5.1 本案例实现

由于本案例是用于解析excel文本框并且需要切割字段,所以本处结合poi进行开发,以及使用json作为响应格式,如果只需要简单实现,请参考5.2

package com.joinus.app;

import net.sf.json.JSONObject;
import org.apache.poi.hssf.usermodel.*;

import javax.activation.DataHandler;
import javax.jws.WebService;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

/**
 * <p>Created with IntelliJ IDEA.</p>
 * <p>User: </p>
 * <p>Date: 2019/11/26</p>
 * <p>Time: 14:19</p>
 *
 * @author 
 * @version 1.0
 * Description: No Description
 */
@WebService
public class ExcelUtil {

    public String  excelForDrawing(String filePath) {

        Map jsonResultMap  = new HashMap();
        String msg = "解析成功";
        String code = "0";
        String details = "" ;
        List<String> resultSheet = new ArrayList();
        List<List<List<String>>> result = new ArrayList<>();
        try {
            File f = new File(filePath);
            HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(f));
            for (int i = 0; i < wb.getNumberOfSheets(); i++) {
                String strResult = "";
                HSSFSheet sheet = wb.getSheetAt(i);
                HSSFPatriarch pat = sheet.getDrawingPatriarch();
                if (null != pat) {
                    List<HSSFShape> children = pat.getChildren();
                    Iterator<HSSFShape> it = children.iterator();
                    while (it.hasNext()) {
                        HSSFShape shape = it.next();
                        if (shape instanceof HSSFTextbox) {
                            HSSFTextbox textbox = (HSSFTextbox) shape;//强转成textbox类型
                            try {
                                textbox.getString();
                            } catch (NullPointerException e) {
                                continue;
                            }
                            HSSFRichTextString richString = textbox.getString(); //取值。。
                            String HSSFTextboxText = richString.getString() + "";

                            if (!HSSFTextboxText.equals("") && !HSSFTextboxText.equalsIgnoreCase("null") && HSSFTextboxText != null) {
                                strResult += HSSFTextboxText + ",";
                            }

                        }
                    }
                }

                resultSheet.add(strResult);
            }
            String reg = "[a-zA-Z]";
            for (String s : resultSheet) {  //遍历sheet页
                List<List<String>> sheetList =  new ArrayList();
                String[] strTempArr = s.split("[\\u4e00-\\u9fa5]");//按照中文切割
                for (int i = 0; i < strTempArr.length; i++) {
                    List<String> temporaryList = new ArrayList();
                    String s1 = strTempArr[i];
                    String[] split = s1.split(",");
                    for (int j = 0; j < split.length; j++) {
                        String s2 = split[j];
                        if (!s2.equals("")&&s2.substring(0,1).matches(reg)) {
                            temporaryList.add(s2);
                        }
                    }
                    if (temporaryList.size()!=0){
                        sheetList.add(temporaryList);
                    }
                }
                result.add(sheetList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            code = "-1";
            msg  ="文件获取出现问题!";
            details = e.getMessage();
        }catch (Exception e1){
            e1.printStackTrace();
            code = "-1";
            msg  ="系统出现异常";
            details = e1.getMessage();
        }

        jsonResultMap.put("code",code);
        jsonResultMap.put("msg",msg);
        jsonResultMap.put("data",result);
        jsonResultMap.put("details",details);

        return  JSONObject.fromObject(jsonResultMap).toString();
    }


}

5.2 简单实现

package com.joinus.app;

import net.sf.json.JSONObject;
import org.apache.poi.hssf.usermodel.*;

import javax.activation.DataHandler;
import javax.jws.WebService;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

/**
 * <p>Created with IntelliJ IDEA.</p>
 * <p>User: </p>
 * <p>Date: 2019/11/26</p>
 * <p>Time: 14:19</p>
 *
 * @author 
 * @version 1.0
 * Description: No Description
 */
@WebService
public class ExcelUtil {

    public String  excelForDrawing(String str) {
        return  "响应结果"+str ;
    }
}

六 启动与调用

可以直接配置tomcat或者打成war包丢入服务器中,在程序发布之后,就可以调用接口了。

接口的调用地址为:项目地址+servlet-mapping中url地址+spring-cxf.xml中jaxws:endpoint的address地址+?wsdl

本例中地址为:http://localhost:8080/ExcelPU/ws/ExcelUtilWB?wsdl  

调用结果如下:

也可以直接访问 项目地址+servlet-mapping中url地址

本例中地址:http://localhost:8080/ExcelPU/ws

调用结果如下:

点击其中wsdl链接跳到第一种调用方式的页面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值