Cxf+spring Publishing WebService and calling

Source: Internet
Author: User

Project Required jar Package: Http://url.cn/V6RvIw because I used the httpclient, it contained the HttpClient jar package.

Build a project and say nothing.

Directly on the code said interface:

Write a demo interface

Package Cn.cxf.demo;import Javax.jws.WebService; @WebServicepublic interface Demo {string Sayhi (string text);}

And then we need to implement it.

TargetNamespace is the package path to the interface

Package Cn.cxf.demo.impl;import Javax.jws.webservice;import Cn.cxf.demo.Demo; @WebService (endpointinterface= " Cn.cxf.demo.Demo ", servicename=" Demoservice ", targetnamespace=" http://demo.cxf.cn/") public class Demoimpl Implements Demo {@Overridepublic string Sayhi (string text) {return "Hi!" + text;}}

Add Applicationcontext.xml in src directory

<?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 "> <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 "/><!--Test interface--><jaxws:endpoint id=" Demoservice "implementor=" Cn.cxf.demo.impl.DemoImpl "address="/demoservice "/></beans>

And then modify the Web. xml file to take care of it (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:

Package Cn.cxf.test;import Org.apache.cxf.endpoint.client;import Org.apache.cxf.jaxws.endpoint.dynamic.jaxwsdynamicclientfactory;public class Cxfwstest {public static void main ( String[] args) throws Exception {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 ());}}

Haha, is not very simple.

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.