Distributed Systems (3)---Web Service combat--CXF Practice

Source: Internet
Author: User
Tags wsdl

Article Two: CXF practice


CXF Architecture Development WebService steps:

1 , establish Web Project

2 , prepare all the Jar Package


3 , Web. XML in the configuration CXF the core Servlet,cxfservlet

Server-side:

< display-name>cxf_demo</display-name><context-param><param-name>contextconfiglocation</ Param-name><param-value>classpath:applicationcontext-server.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><welcome-file-list><welcome-file >index.jsp</welcome-file></welcome-file-list> 


4 , Applicationcontext-server.xml

Server-side

<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= "HelloService" implementor= "Com.test.server.HelloWorldServerImpl" address= "/helloservice"/>


Client

<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 "/><bean id=" client "Class=" Com.test.server.IHelloWorldServer "factory-bean=" clientfactory "factory-method=" create "/><bean id=" Clientfactory "class=" Org.apache.cxf.jaxws.JaxWsProxyFactoryBean "><property name=" ServiceClass "value=" Com.test.server.IHelloWorldServer "/><property name=" Address "value=" http://localhost:8080/cxf_demo/ws/ HelloService "/></bean>

CXF There are two classes for publishing a service:

Jaxwsserverfactorybean we use this. This class is used to publish a service that can be constructed by default.

Jaxrsserverfactorybean , this class is used to publish Restful style of WebService ; Restful style is a common Get,post The request is standard and can be requested and the corresponding JSON data.

5 , Code

Server-side, publishing services

Ihelloworldserver

@WebServicepublic interface Ihelloworldserver {public string SayHello (string username);

Helloworldserverimpl

@WebService (endpointinterface = "Com.test.server.IHelloWorldServer", Servicename= "HelloService") public class Helloworldserverimpl implements ihelloworldserver{@Overridepublic string SayHello (string username) {return username + " : HelloWorld ";}}


Client

Helloworldclient

public static void Main (string[] args) {ApplicationContext context = new Classpathxmlapplicationcontext (" Applicationcontext-client.xml "); Ihelloworldserver HelloService = (ihelloworldserver) Context.getBean (" Client "); String response = Helloservice.sayhello ("Liutengteng"); SYSTEM.OUT.PRINTLN (response);}

6 , running results

Access Address: Http://localhost:8080/cxf_demo/ws


WSDL :



Client Run Results:



Summarize

From the simple example above we can easily see that the remote call is through the server-side Publishing Service, the client calls. The published WSDL is presented in XML form, XML parsing, and SOAP is XML-based. Because XML is common in various languages, Web service implements cross-platform, cross-language.

Distributed Systems (3)---Web Service combat--CXF Practice

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.