1.使用cxf生成webservice,引入cxf jar包,在pom.xml中添加依赖即可:
<!--CXF--!>
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-bundle</artifactId> </dependency>
2.定义一个webservice服务的客户端:
com.abc.webservice
import javax.jws.WebMethod; import javax.jws.WebResult; import javax.jws.WebService; /** * 毕业生认证webservice服务的客户端 * * @author * */ @WebService(targetNamespace = "http://www.abc.com") public interface BysRzServerService { /** * 查询毕业生信息(毕业生认证) * * @param xm * 姓名 * @param byzh * 毕业生号 * @return RspBean */ @WebMethod(operationName = "bysRz") @WebResult(name = "result") public String bysRz(String xm,String byzh) throws Exception; }
注:WebMethod(operationName="bysRz")即为webservice的方法名,WebResult(name="result")表示webservice返回值
@WebService(endpointInterface="com.abc.webservice.BysRzServerService") public class BysRzServerServiceImpl implements BysRzServerService { private static final Log log = LogFactory.getLog(BysRzServerServiceImpl.class); /** * 毕结业综合查询service */ @Override public String bysRz(String xm, String byzh) throws Exception { //进行业务逻辑处理,将处理结果返回 StringBuffer sb=new StringBuffer(""); return sb.toString(); } }
3.在spring-webservice.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" xmlns:http-conf="http://cxf.apache.org/transports/http/configuration" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd "> <!--认证webservice --> <jaxws:server id="bysRzServerService" serviceClass="com.abc.webservice.BysRzServerService" address="/bysRzServerService" > <jaxws:serviceBean> <bean class="com.abc.webservice.BysRzServerServiceImpl"> <property name="bysZhcxService" ref="bysZhcxService"/><!--该处为业务逻辑处理需要使用的bean对象--> </bean>
<!--webservice拦截器,可以对调用webservice的客户端进行验证-->
<jaxws:inInterceptors>
<bean class="com.cattsoft.baseplatform.func.sm.web.WebserviceAuthenticationInterceptor">
<property name="sysParamCacheManager" ref="sysParamCacheManager"/>
</bean>
</jaxws:inInterceptors>
</jaxws:serviceBean> </jaxws:server>
//另一种配置方法
<jaxws:endpoint id="bysRzServerService"
implementorClass="com.abc.webservice.BysRzServerService"
address="/bysRzServerService">
<jaxws:implementor>
<bean class="com.abc.webservice.BysRzServerServiceImpl">
<property name="imDealDeptUserService" ref="imDealDeptUserService" />
</bean>
</jaxws:implementor>
</jaxws:endpoint>
</beans>
4.将spring-webservice.xml文件引入到applicationContext.xml中,在web.xml中配置CXF的servlet配置:
<!-- CXF --> <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>/webservice/*</url-pattern> </servlet-mapping>
5.通过ip可以测试webservice是否成功:http://localhost:8088/项目名称/webservice/bysRzServerService?wsdl
6.通过AXIS调用webservice
//service所在的URL String endpoint= "http://localhost:8088/zhongzhi/webservice/ideaUserServerService"; //加入调用webservice用户验证信息 String namespace="";//命名空间 SOAPHeaderElement header = new SOAPHeaderElement(namespace,"Authentication"); header.setPrefix(""); header.addChildElement("username").addTextNode("admin"); header.addChildElement("password").addTextNode("admin"); //创建一个服务(service)调用(call) Service service =new Service(); Call call = (Call)service.createCall();//通过service创建call对象 call.addHeader(header); //设置service所在URL call.setTargetEndpointAddress(new URL(endpoint)); //设置命名空间,webservice方法名称 call.setOperationName(new QName("http://www.cattsoft.com","synchronizeIdeaUser")); //设置参数 call.addParameter("userJson", XMLType.XSD_STRING, ParameterMode.IN); call.addParameter("sysDeptJson", XMLType.XSD_STRING, ParameterMode.IN); call.setReturnType(XMLType.XSD_STRING); //将两个实体类转换为json字符串进行传递 String userJson = JSONObject.fromObject(ideaUser).toString(); String sysDeptJson = JSONObject.fromObject(sysDept).toString(); call.setUseSOAPAction(true); //调用webservice方法,传递参数,多个参数以数组形式传递 String result = (String)call.invoke(new Object[]{userJson,sysDeptJson}); Log.debug("国网同步省网数据结果:"+result);
7.对应的webservice服务端加入拦截器,进行调用webservice服务的验证
package com.cattsoft.baseplatform.func.sm.web; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.xml.soap.SOAPException; 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.apache.cxf.transport.http.AbstractHTTPDestination; import org.w3c.dom.Node; import com.cattsoft.baseplatform.cache.SysParamCacheManager; import com.cattsoft.zhongzhi.core.utils.Constants; import com.tongtech.backport.java.util.Arrays; public class WebserviceAuthenticationInterceptor extends AbstractPhaseInterceptor<SoapMessage>{ private SysParamCacheManager sysParamCacheManager; public WebserviceAuthenticationInterceptor() { super(Phase.PRE_INVOKE); } @Override public void handleMessage(SoapMessage msg) throws Fault { HttpServletRequest request=(HttpServletRequest)msg.get( AbstractHTTPDestination.HTTP_REQUEST); String url =request.getRequestURL().toString(); String ip = url.substring(url.indexOf("/")+2,url.lastIndexOf(":")); //获取数据库配置的允许访问的IP地址 String ips= sysParamCacheManager.getEnableParamValue(Constants.ServerInfo.WEBSERVICE_ALLOWED_IP); List<String> list = Arrays.asList(ips.split(",")); if(!list.contains(ip)){ System.out.println("webservice调用异常-----未经授权的IP调用"); throw new Fault(new SOAPException("未经授权的IP调用webservice")); } } public SysParamCacheManager getSysParamCacheManager() { return sysParamCacheManager; } public void setSysParamCacheManager(SysParamCacheManager sysParamCacheManager) { this.sysParamCacheManager = sysParamCacheManager; } }