采用spring,maven,xfire/cxf 实现SOAP的WebService,并用C#作为客户端调用

之前不懂xfire是什么,在网上搜了半天才知道,xfire就是现在的cxf。 由于xfire在几年前就已停止更新了,所以这里采用cxf来实现webService.下文主要是针对soap的.


1.服务端开发

1.1cxf相关maven配置

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.haiyang</groupId>
	<artifactId>zhushoumao</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<!-- 集中定义依赖版本号 -->
	<properties>
		<junit.version>4.12</junit.version>
		<spring.version>4.1.3.RELEASE</spring.version>
		<mybatis.version>3.2.8</mybatis.version>
		<mybatis.spring.version>1.2.2</mybatis.spring.version>
		<mybatis.paginator.version>1.2.15</mybatis.paginator.version>
		<mysql.version>5.1.32</mysql.version>
		<slf4j.version>1.6.4</slf4j.version>
		<jackson.version>2.4.2</jackson.version>
		<druid.version>1.0.9</druid.version>
		<httpclient.version>4.3.5</httpclient.version>
		<jstl.version>1.2</jstl.version>
		<servlet-api.version>2.5</servlet-api.version>
		<jsp-api.version>2.0</jsp-api.version>
		<joda-time.version>2.5</joda-time.version>
		<commons-lang3.version>3.3.2</commons-lang3.version>
		<commons-io.version>1.3.2</commons-io.version>
		<commons-net.version>3.3</commons-net.version>
		<pagehelper.version>3.4.2-fix</pagehelper.version>
		<jsqlparser.version>0.9.1</jsqlparser.version>
		<commons-fileupload.version>1.3.1</commons-fileupload.version>
		<jedis.version>2.7.2</jedis.version>
		<solrj.version>4.10.3</solrj.version>
		<zxing.version>3.2.1</zxing.version>
		<fastjson.version>1.1.15</fastjson.version>
		<jsoup.version>1.9.1</jsoup.version>
		<zhushoumao-other.version>0.0.1-SNAPSHOT</zhushoumao-other.version>
		<cxf.version>3.1.1</cxf.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>com.haoqinshi</groupId>
			<artifactId>haoqinshi-common</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<scope>test</scope>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<!-- JSP相关 -->
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>${jstl.version}</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>${servlet-api.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jsp-api</artifactId>
			<version>${jsp-api.version}</version>
			<scope>provided</scope>
		</dependency>
		<!-- 文件上传组件 -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>${commons-fileupload.version}</version>
		</dependency>
		<!-- Mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>${mybatis.version}</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>${mybatis.spring.version}</version>
		</dependency>
		<dependency>
			<groupId>com.github.miemiedev</groupId>
			<artifactId>mybatis-paginator</artifactId>
			<version>${mybatis.paginator.version}</version>
		</dependency>
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>${pagehelper.version}</version>
		</dependency>
		<!-- MySql -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql.version}</version>
		</dependency>
		<!-- 连接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>${druid.version}</version>
		</dependency>
		<!-- json -->
		<dependency>
			<groupId>org.json</groupId>
			<artifactId>json</artifactId>
			<version>20131018</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>${fastjson.version}</version>
		</dependency>
		<dependency>
			<groupId>org.jsoup</groupId>
			<artifactId>jsoup</artifactId>
			<version>${jsoup.version}</version>
		</dependency>
		<!-- Jackson Json处理工具包 -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>${jackson.version}</version>
		</dependency>
		<!-- cxf -->
		<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>cglib</groupId>
			<artifactId>cglib-nodep</artifactId>
			<version>2.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.ws.xmlschema</groupId>
			<artifactId>xmlschema-core</artifactId>
			<version>2.0</version>
		</dependency>
	</dependencies>
	<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
	<build>
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
				</includes>
				<filtering>false</filtering>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
				</includes>
				<filtering>false</filtering>
			</resource>
		</resources>
		<plugins>
			<!-- 配置Tomcat插件 -->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<url>http://192.168.1.100/manager/text</url>
					<path>/</path>
					<server>tomcat</server>
					<username><span style="font-family: Arial, Helvetica, sans-serif;">tomcat</span></username>
					<password>tomcat</password>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

这样,jar包就添加好了。

1.2 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="zhushoumao" version="2.5">
	<display-name>zhushoumao</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</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>
	<!-- 加载spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/applicationContext-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 解决post乱码 -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- 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>/webservice/*</url-pattern>
	</servlet-mapping>
</web-app>


1.3 webService服务类开发

首先是服务接口类:
package com.zhushoumao.webservice;

import javax.jws.WebMethod;
import javax.jws.WebService;

import com.zhushoumao.bianyi.controller.Analyzer;
import com.zhushoumao.bianyi.pojo.Gs;

/**
 * @author PuHaiyang
 * @createTime 2016年6月13日 下午3:42:56
 * @email [email protected]
 * @function ll1文法service
 *
 */
@WebService
public interface Ll1Service {
	/**
	 * 获取预测分析表
	 * 
	 * @param ll1GsStr
	 *            LL1文法字符器,由;分格
	 * @return
	 */
	@WebMethod
	public Gs getGsAnalyzeTable(String ll1GsStr);

	/**
	 * 获取句子分析
	 * 
	 * @param ll1GsStr
	 *            ll1文法
	 * @param startChar
	 *            开始符
	 * @param expStr
	 *            句子
	 * @return
	 */
	@WebMethod
	public Analyzer getExpAnalyze(String ll1GsStr, Character startChar, String expStr);
}

实现类:
package com.zhushoumao.webservice.impl;

import java.util.ArrayList;

import javax.jws.WebService;

import com.zhushoumao.bianyi.controller.Analyzer;
import com.zhushoumao.bianyi.pojo.AnalyzeProduce;
import com.zhushoumao.bianyi.pojo.Gs;
import com.zhushoumao.webservice.Ll1Service;

/**
 * @author PuHaiyang
 * @createTime 2016年6月13日 下午3:43:34
 * @email [email protected]
 * @function Ll1ServiceImpl实现类
 *
 */
@WebService(serviceName = "ll1Service", endpointInterface = "com.zhushoumao.webservice.Ll1Service")
public class Ll1ServiceImpl implements Ll1Service {

	public Gs getGsAnalyzeTable(String ll1GsStr) {
		Gs gs = new Gs();
		gs.setS('E');
		return gs;
	}

	public Analyzer getExpAnalyze(String ll1GsStr, Character startChar, String expStr) {

		Analyzer analyzer = new Analyzer();
		analyzer.setStartChar(startChar);
		return analyzer;
	}

}


1.4 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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 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://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
          http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

	<bean id="Ll1ServiceImpl" class="com.zhushoumao.webservice.impl.Ll1ServiceImpl" />

	<jaxws:server address="/Ll1Service"
		serviceClass="com.zhushoumao.webservice.Ll1Service">
		<jaxws:serviceBean>
			<ref bean="Ll1ServiceImpl" />
		</jaxws:serviceBean>
	</jaxws:server>

</beans>


1.5 查看WebService

之后再运行Maven工程。
成功后,在浏览器中打开:http://localhost:8080/webservice

好,这样就证明发布服务成功了。接下来就可以用第三方来调用了

2.客户端开发

2.1 c#客户端远程调用

对于它的调用就比较简单了。如果用java作客户端的话,可以用java自带的工具(wsimport )生成jar进行连接。

这里为了方便,我采用vs2012中的工具来生成远程连接的代码。

打开vs的项目,在资源管理器的项目中右键,选择”添加服务引用“,在打开的界面中输入服务的地址:即wsdl的地址,如:http://localhost:8080/webservice/Ll1Service?wsdl

采用spring,maven,xfire/cxf 实现SOAP的WebService,并用C#作为客户端调用_第1张图片

采用spring,maven,xfire/cxf 实现SOAP的WebService,并用C#作为客户端调用_第2张图片

点击确定后,就会生成一个引用了。采用spring,maven,xfire/cxf 实现SOAP的WebService,并用C#作为客户端调用_第3张图片

由于之前把命名空间写的是:LL1Service,所以就会生成LL1Service这个引用了。

生成后,我们再来调用试一下吧。

在这里,我用c#的winForm拖一个界面出来,然后再其中添加一个按钮,双击按钮,进入按钮的点击事件。其中调用代码如下:

        private void ll1_gs_analyze_Click(object sender, EventArgs e)
        {
            string gsStr = tb_ll1_gs.Text.Trim();
            MessageBox.Show(gsStr);
            LL1Service.Ll1ServiceClient service = new LL1Service.Ll1ServiceClient();
            MessageBox.Show(service.getGsAnalyzeTableJson(gsStr));
              
        }

采用spring,maven,xfire/cxf 实现SOAP的WebService,并用C#作为客户端调用_第4张图片


这样,成功调用了远程的代码了。

你可能感兴趣的:(webservice,CXF,SOA)