转载请注明出处:http://blog.csdn.net/u013474104/article/details/45097957
步骤— —
1.使用Maven依赖jar文件
2.web.xml配置
a.加载cxf-beans.xml配置文件
b.配置CXF Servlect
3.cxf-beans.xml服务配置
a.lics2uldwms.java服务接口类
b.lics2uldwms.java服务接口的实现类
4.访问服务接口
5.Client调用服务接口——TestDemo.java类
————————————————————————————————————————
1.使用Maven依赖jar文件
<!-- 增加cxf依赖 start--> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.11</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.7.11</version> </dependency> <!-- 增加cxf依赖 end-->
2.web.xml配置
a.加载cxf-beans.xml配置文件
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:conf/applicationContext.xml,classpath:conf/cxf-beans.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
b.配置CXF Servlect
<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>
3.cxf-beans.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:context="http://www.springframework.org/schema/context" xmlns:http-conf="http://cxf.apache.org/transports/http/configuration" xsi:schemaLocation=" http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd "> <!-- Import apache CXF bean definition 固定 --> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <!-- CXF发送、接收消息超时设置,对所有的服务生效 ConnectionTimeout:连接超时 ReceiveTimeout:接收超时 --> <!-- <http-conf:conduit name="*.http-conduit"> <http-conf:client ConnectionTimeout="1000" ReceiveTimeout="100"/> </http-conf:conduit> --> <!-- 发布服务 --> <bean id="lics2uldwmsBean" class="com.ws.lics2uldwms.lics2uldwmsImpl" /> <jaxws:server id="lics2uldwms" address="/lics2uldwms" serviceClass="com.ws.lics2uldwms.lics2uldwms"> <jaxws:serviceBean> <ref bean="lics2uldwmsBean"/> </jaxws:serviceBean> </jaxws:server> </beans>
a.lics2uldwms.java服务接口类
package com.ws.lics2uldwms; import javax.jws.WebMethod; import javax.jws.WebService; @WebService //@SOAPBinding(style=Style.RPC) /*@SOAPBinding(style = Style.RPC) 动态调用JaxWsDynamicClientFactory 不支持该注解*/ public interface lics2uldwms { /** *1 获取物资编目 * * @param msg * @return */ @WebMethod public String reqGoods(String msg); }
b.lics2uldwms.java服务接口的实现类
package com.ws.lics2uldwms; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import javax.jws.WebService; import com.hidp.common.Constant; import com.utils.DateTimeUtils; import com.wms.service.baseinfo.ProductService; import com.ws.xml.CheckHeader; import com.ws.xml.FormXmlDom4J; @WebService(endpointInterface = "com.ws.lics2uldwms.lics2uldwms", serviceName = "lics2uldwmsImpl") public class lics2uldwmsImpl implements lics2uldwms { @Resource ProductService productService; private String sstarTime=""; private String sendTime=""; /** * 1获取物资编目 * * @param msg * @return */ @SuppressWarnings("rawtypes") @Override public String reqGoods(String msg) { sstarTime=DateTimeUtils.getCurrentDateTime(); String rmsg = ""; try { if (!CheckHeader.checkHeader(msg,"uld")) { Constant.log4j.error("方法名称:reqGoods,失败原因:用户名密码验证不通过"); return FormXmlDom4J.generateStateXml("1", "失败:用户名密码验证不通过"); } List ls = new ArrayList<>(); ls = productService.queryProductWs(); rmsg = FormXmlDom4J.generateProductXml(ls); } catch (Exception e) { Constant.log4j.error("方法名称:reqGoods,失败原因:"+e.toString()); return FormXmlDom4J.generateStateXml("1", "失败:" + e.toString()); } sendTime=DateTimeUtils.getCurrentDateTime(); //记录响应时间 Constant.log4j.info("方法名称:reqGoods,起始时间:"+sstarTime+",结束时间:"+sendTime); return rmsg; } }
4.访问服务接口
地址输入下面之前自定义的地址,回车— —
点击上述链接后,将跳转到wsdl地址,TestDemo.java中我们需要的是wsdl的地址
5.Client调用服务接口——TestDemo.java类
package com.ws.demo.wsservice; import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import org.dom4j.Document; import org.dom4j.io.SAXReader; /** * client类 * @author ckz * */ public class TestDemo { public static void main(String[] args) throws Exception { JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient("http://localhost:8081/lics/ws/fwms2assist?wsdl"); boolean outTimeSate = false; Object[] res =null; try{ /*//设置 CXF客户端配置请求超时限制 HTTPConduit http = (HTTPConduit) client.getConduit(); HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy(); //连接超时设置 httpClientPolicy.setConnectionTimeout(1000); httpClientPolicy.setAllowChunking(false); //接收超时设置 httpClientPolicy.setReceiveTimeout(5000); http.setClient(httpClientPolicy);*/ //读取xml文件的doc对象,Fwms2Assist_reqProduct.xml为传入的参数 String filePath = TestDemo.class.getClassLoader().getResource("Fwms2Assist_reqGoods.xml").getPath(); SAXReader reader = new SAXReader(); Document doc = reader.read(filePath); //调用接口 res = client.invoke("reqGoods", doc.asXML()); }catch(Exception ex){ outTimeSate=true; ex.printStackTrace(); } if(outTimeSate){ System.out.println("服务超时!"); }else{ System.out.println(com.utils.BASE64Util.geDecoderStr((String) res[0])); } } }
时间: 2024-10-28 20:12:34