WebService与Spring整合部署
Posted maysky_l
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebService与Spring整合部署相关的知识,希望对你有一定的参考价值。
服务端(CXF发布webservice):
1、 http://cxf.apache.org/download.html下载 cxf文件包(3.1.18必须使用Java 7或Java 8)。
2、 将下载包的lib文件夹下的jar全部拷贝到spring项目中的lib目录下,注意删除相同的jar包(版本号不同拷贝不会替换)。
3、 删除以下4个jar包:
cxf-services-ws-discovery-api-3.1.5.jar
cxf-services-ws-discovery-service-3.1.5.jar
cxf-services-wsn-api-3.1.5.jar
cxf-services-wsn-core-3.1.5.jar
4、 web.xml配置如下:
<!-- 配置cxf框架 cxf-servlet.xml --> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <init-param> <param-name>config-location</param-name> <param-value>classpath:conf/cxf-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/webservice/*</url-pattern> </servlet-mapping>
5、 conf目录下添加cxf-servlet.xml文件, cxf-servlet.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="helloService" address="/MyWebService" implementor="net.evecom.hr.MyWebService"></jaxws:endpoint> </beans>
7、 在net.evecom.hr目录下创建MyWebService类,如下:
package net.evecom.hr; import javax.jws.WebParam; import javax.jws.WebService; @WebService public class MyWebService { public String hiMyService(@WebParam(name = "content") String content) { System.out.println("客户端发送过来了:" + content); return "服务器返回:" + content; } }
8、启动tomcat,访问localhost:8080/项目名/webservice/,查看是否正常启动。
客户端(axis2获取webservice):
1、 http://axis.apache.org/axis2/java/core/download.html下载axis2-1.7.9;
2、 将下载包lib文件夹下的jar包复制到另一个项目lib目录下;
3、 测试代码如下:
package net.evecom.hr; import java.util.Iterator; import javax.xml.namespace.QName; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; import com.alibaba.fastjson.JSONObject; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub String url = "http://localhost:8080/项目名/webservice/MyWebService?wsdl"; sendWsdl(url, "啊啊啊啊啊"); } public static void sendWsdl(String url, String xmlStr) { JSONObject res = new JSONObject(); try { String endpoint = url.replace("?wsdl", "");// 不需要传?wsdl // 直接引用远程的wsdl文件 Options options = new Options(); options.setTo(new EndpointReference(endpoint)); ServiceClient sender = new ServiceClient(); sender.setOptions(options); OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace("http://hr.evecom.net/", "");// 参数1(uri)=即为wsdl文档的targetNamespace;参数2(prefix)=可不填 OMElement method = fac.createOMElement("hiMyService", omNs);// 方法名 QName qname = new QName("content"); OMElement in0 = fac.createOMElement(qname);// 方法参数 in0.setText(xmlStr); // 参数值 method.addChild(in0); // 添加参数 OMElement resultEle = sender.sendReceive(method);// 调用wsdl System.out.println("调用接口结果:" + resultEle.toString()); Iterator<?> iterator = resultEle.getChildElements(); OMElement result1 = null; while (iterator.hasNext()) { result1 = (OMElement) iterator.next(); System.out.println(result1.getText()); } } catch (AxisFault ex) { ex.printStackTrace(); } } }
4、运行代码,查看是否正常返回
至此,简单的webservice与Spring整合部署完成。
=====================================我是一条分割线============================================
添加拦截器:
- 服务端配置如下:
a) cxf-servlet.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"> <bean id="webServiceInterceptor" class="net.evecom.common.interceptors.WebServiceInterceptor"></bean> <bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> <bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> <!-- 加载提供的服务 --> <jaxws:endpoint id="webServiceTest" address="/MyWebService" implementor="net.evecom.webservice.hr.MyWebService"> <!-- 添加in拦截器 --> <jaxws:inInterceptors> <ref bean="loggingInInterceptor" /> <ref bean="webServiceInterceptor" /> </jaxws:inInterceptors> <!-- 添加out拦截器 --> <jaxws:outInterceptors> <ref bean="loggingOutInterceptor" /> </jaxws:outInterceptors> </jaxws:endpoint> <!-- <jaxws:server id="webServiceTest" serviceClass="net.evecom.webservice.hr.MyWebService" address="/MyWebService"> <jaxws:serviceBean> <ref bean="MyWebService" /> </jaxws:serviceBean> </jaxws:server> --> </beans>
b) 添加WebServiceInterceptor,代码如下:
/* * Copyright (c) 2005, 2016, EVECOM Technology Co.,Ltd. All rights reserved. * EVECOM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * */ package net.evecom.common.interceptors; import java.util.List; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.headers.Header; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; import org.w3c.dom.Element; import net.evecom.common.util.MD5; public class WebServiceInterceptor extends AbstractPhaseInterceptor<SoapMessage> { private static final String TOKEN = "test"; public WebServiceInterceptor(String phase) { super(phase); } public WebServiceInterceptor() { super(Phase.PRE_INVOKE); } public void handleMessage(SoapMessage message) throws Fault { List<Header> headers = message.getHeaders(); if (headers == null || headers.size() == 0) { throw new Fault(new IllegalArgumentException("没有Header,拦截器实施拦截")); } Header firstHeader = headers.get(0); Element ele = (Element) firstHeader.getObject(); String token = ele.getFirstChild().getTextContent(); if (!token.equals(MD5.parseMD5(TOKEN))) { throw new Fault(new IllegalArgumentException("token不正确")); } } }
- 客户端需要在发送请求时,添加头消息(token=md5.parseMD5(“test”)),代码如下:
// 创建消息头:token=evecomoa SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory(); // SOAPHeaderBlock soapHeader = soapFactory.createSOAPHeaderBlock("RequestHeader", omNamespace); // 添加头消息内容 SOAPHeaderBlock soapHeaderBlock = soapFactory.createSOAPHeaderBlock("token", omNamespace); soapHeaderBlock.addChild(soapFactory.createOMText(MD5.parseMD5("test"))); // soapHeader.addChild(soapHeaderBlock); // 将消息头加入到当前消息中 sender.addHeader(soapHeaderBlock);
以上是关于WebService与Spring整合部署的主要内容,如果未能解决你的问题,请参考以下文章
WebServicecxf webService与Spring整合
WebServicecxf webService与Spring整合