Web App uses Apache CXF to build webservice--detailed case (combined with spring)

Source: Internet
Author: User
Tags wsdl

Reprint Please specify source: http://blog.csdn.net/u013474104/article/details/45097957

Steps--

1. using MAVEN to rely on jar files

2.Web. XML Configuration

A. loading The cxf-beans.xml configuration file

B. configuring CXF Servlect

3.Cxf-beans.xml Service Configuration

A.Lics2uldwms.java Service Interface class

B.implementation classes for the Lics2uldwms.java service interface

4. Accessing the Service interface

5.Client calling the service interface--testdemo.java class

————————————————————————————————————————

1. Using Maven to rely on jar files

<!--increase CXF dependency  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>    <!--increase CXF dependency  end-->
2.web.xml Configuration

A. Loading the Cxf-beans.xml configuration file

<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. Configuring 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 Service Configuration

<?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.xsdhttp:// Cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd ">< !--Import Apache CXF bean definition fixed--><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 Send, receive message timeout settings for all services ConnectionTimeout: Connection Timeout ReceiveTimeout: Receive timeout--<!--< Http-conf:conduIt name= "*.http-conduit" >A.lics2uldwms.java Service Interface Class

Package Com.ws.lics2uldwms;import Javax.jws.webmethod;import Javax.jws.WebService; @WebService//@SOAPBinding (style =STYLE.RPC)/* @SOAPBinding (Style = style.rpc) Dynamic call Jaxwsdynamicclientfactory does not support this annotation */public interface Lics2uldwms {/** * 1 Get Material Catalog *  * @param msg * @return */@WebMethodpublic string Reqgoods (String msg);}
B.Lics2uldwms.java Implementation class for the service interface

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 {@ResourceProductService productservice;private string sstartime= "";p rivate string Sendtime= "" ;/** * 1 Get Material Catalog * * @param msg * @return */@SuppressWarnings ("Rawtypes") @Overridepublic string Reqgoods (String msg) {Sstar Time=datetimeutils.getcurrentdatetime (); String rmsg = ""; try {if (! Checkheader.checkheader (msg, "Uld")) {Constant.log4j.error ("Method name: Reqgoods, Failure reason: User name password verification does not pass"); return Formxmldom4j.generatestatexml ("1", "failed: User name password Authentication not passed");} List ls = new arraylist<> (); ls = Productservice.queryproductws (); rmsg = Formxmldom4j.generateproductxmL (LS);} catch (Exception e) {Constant.log4j.error ("Method name: reqgoods, reason for failure:" +e.tostring ()); return Formxmldom4j.generatestatexml ("1", "failed:" + e.tostring ());} Sendtime=datetimeutils.getcurrentdatetime ();//Record Response time Constant.log4j.info ("Method name: Reqgoods, start time: +sstartime+", End time : "+sendtime); return rmsg;}}

4. Accessing the Service interface

Address enter the previously customized address, enter--


Click on the link above, will jump to the WSDL address, Testdemo.java We need is the WSDL address


5.Client calling the service interface--testdemo.java class

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 class * @author Ckz * */public class Testdemo {public static void main (string[] args) throw s Exception {jaxwsdynamicclientfactory DCF = jaxwsdynamicclientfactory.newinstance (); Client client = dcf.createclient ("http://localhost:8081/lics/ws/fwms2assist?wsdl"); Boolean outtimesate = False;o     Bject[] Res =null;try{/*//set CXF Client configuration request time-out limit httpconduit http = (httpconduit) client.getconduit ();    Httpclientpolicy httpclientpolicy = new Httpclientpolicy ();     Connection Timeout setting httpclientpolicy.setconnectiontimeout (1000); Httpclientpolicy.setallowchunking (FALSE); Receive timeout setting httpclientpolicy.setreceivetimeout (5000); Http.setclient (httpclientpolicy); *///reads the Doc object of the XML file, fwms2assist_reqproduct.xml the passed argument string filePath = TestDemo.class.getClassLoader (). GetResource ("Fwms2assist_reqgoods.xml"). GetPath (); Saxreader ReaDer = new Saxreader ();D ocument doc = Reader.read (filePath);//Call Interface res = Client.invoke ("Reqgoods", Doc.asxml ());} catch (Exception ex) {outtimesate=true;ex.printstacktrace ();} if (outtimesate) {System.out.println ("Service timed out! ");} Else{system.out.println (Com.utils.BASE64Util.geDecoderStr ((String) res[0]);}}}



Web App uses Apache CXF to build webservice--detailed case (combined with spring)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.