WebService CXF Instances

Source: Internet
Author: User
Tags wsdl

Switch from Baidu Space: Http://hi.baidu.com/cpuhandou/item/b8b439860afb99c9ee083d65

Introduction to CXF WebService development

1. Create a new javawebproject, named Cxfdemo

Select "Next" to add the Userlib library to project.

2. Open the new project and create a new package com.handou.cxf.test under SRC. Create a new inteface name in the package called HelloWorld
The code is as follows:
@WebService
Public interface HelloWorld {
@WebMethod
public string SayHello (@WebParam (name= "name") string name);

}

Create a new implementation class for this interface, with the following code:
public class Helloworldimpl implements HelloWorld {
    public String SayHello (string name) {
      //TODO auto-generated method stub
        return Name.concat (", Hello WebService!!!");
   }
}

3. Release WebService.
Use the CXF built-in Jetty Server Publishing Service. Add the main () method to the Helloworldimpl implementation class, as follows:
public static void Main (string[] args) {
Helloworldimpl implementor = new Helloworldimpl ();
Jaxwsserverfactorybean svrfactory = new Jaxwsserverfactorybean ();
Svrfactory.setserviceclass (Helloworld.class);
Svrfactory.setaddress ("Http://localhost:8080/helloWorld");
Svrfactory.setservicebean (implementor);
Svrfactory.getininterceptors (). Add (New Loggingininterceptor ());
Svrfactory.getoutinterceptors (). Add (New Loggingoutinterceptor ());
Svrfactory.create ();
}

4. Enter HTTP://LOCALHOST:8080/HELLOWORLD?WSDL in the Address bar to view the WSDL details




5. Access the WebService server.
Method 1: The WSDL indicates that the interface has only one operation for invocation, the method name is SayHello, the input parameter variable is name, the data type is string, and the address bar URL is used? + parameter test, input:
Http://localhost:8080/helloWorld/sayHello?name=HanDou, displaying return information

Method 2: Client-side encoding is called.
Create a new Java class helloclient with the following code:
public class Helloclient {
/**
* @param args
*/
public static void Main (string[] args) {
Jaxwsproxyfactorybean factory = new Jaxwsproxyfactorybean ();
Factory.getininterceptors (). Add (New Loggingininterceptor ());
Factory.getoutinterceptors (). Add (New Loggingoutinterceptor ());
Factory.setserviceclass (Helloworld.class);
Factory.setaddress ("Http://localhost:8080/helloWorld");
HelloWorld client = (HelloWorld) factory.create ();
String reply = Client.sayhello ("Handou");
SYSTEM.OUT.PRINTLN ("Server said:" + reply);
System.exit (0);
}
}

The console displays the feedback message:



6. Release WebService with SPRING+CXF integration.

1. Add the CXF configuration in the new Beans.xml,beans.xml under Web-inf.
<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"/>

<jaxws:endpoint
Id= "HelloWorld"
Implementor= "Com.handou.cxf.test.HelloWorldImpl"
address= "/helloworld"/>
</beans>

2. Add spring and CXF configuration to Web. xml
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>

    <listener>
       <listener-class>
            Org.springframework.web.context.ContextLoaderListener
       </listener-class>
    </listener>

<servlet>
<servlet-name>CXFServlet</servlet-name>
<DISPLAY-NAME>CXF servlet</display-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>/*</url-pattern>
</servlet-mapping>
</web-app>

3. Deploy the file to the server, start the server, and +webservice the service name by IP address + port + project name.
Visit the project first,

Click on WSDL to enter HTTP://LOCALHOST:8080/CXF/HELLOWORLD?WSDL to get WSDL details

#webservice/xml/esb

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.