Cxf+spring Publishing WebService and calling

Source: Internet
Author: User

Project Required jar Package: Http://files.cnblogs.com/files/walk-the-Line/cxf-spirng.zip

First write a demo interface

   Package Import Javax.jws.WebService, @WebService public interface Demo {    string Sayhi (string text);}

And then you need its implementation class.

TargetNamespace is the package path to the interface

Package Importimport Cn.cxf.demo.Demo; @WebService (endpointinterface = "Cn.cxf.demo.Demo", servicename= "Demoservice", targetnamespace= "http://demo.cxf.cn/")public  classimplements  Demo {     @Override    public  String sayhi ( String text) {        return "Hi!" + text;    }}

Add Applicationcontext.xml in src directory

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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 ">      <ImportResource= "Classpath:meta-inf/cxf/cxf.xml" />    <ImportResource= "Classpath:meta-inf/cxf/cxf-extension-soap.xml" />    <ImportResource= "Classpath:meta-inf/cxf/cxf-servlet.xml" />         <!--Test Interface -    <Jaxws:endpointID= "Demoservice"implementor= "Cn.cxf.demo.impl.DemoImpl"Address= "/demoservice" />     </Beans>

then modify the Web. xml file to get it done ~ (add the following on Web. xml)

<Context-param>    <Param-name>Contextconfiglocation</Param-name>    <Param-value>Classpath:applicationContext.xml</Param-value>  </Context-param>  <Listener>    <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class>  </Listener>  <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>/ws/*</Url-pattern>  </servlet-mapping>

Publish it and visit Http://localhost:8080/cxfdemo/ws to see that the interface has been published successfully.

It's much easier to call the interface.

If you simply call the interface, you only need 4 jar packages:

Cxf-2.4.3.jar

Neethi-3.0.1.jar

Wsdl4j-1.6.2.jar

Xmlschema-core-2.0.1.jar

Can be selected from the jar package above.

The calling code is as follows:

 Packagecn.cxf.test;Importorg.apache.cxf.endpoint.Client;Importorg.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; Public classCxfwstest { Public Static voidMain (string[] args)throwsException {jaxwsdynamicclientfactory Factory=jaxwsdynamicclientfactory.newinstance (); Client Client= Factory.createclient ("http://localhost:8080/cxfdemo/ws/DemoService?wsdl"); Object[] Objs= Client.invoke ("Sayhi", "Ford"); System.out.println (objs[0].tostring ()); }}

Cxf+spring Publishing WebService and calling

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.