Cxf+spring Build WebService

Source: Internet
Author: User
Tags http post soap wsdl

WebService:

  WebService is a set of standards, not a specific technology. Different platforms, different languages, mostly provide the development and implementation of WebService.

On the surface, Webservice is an application that exposes an API that can be called through the Web. In other words, this application can be invoked programmatically through the Web.

A more precise explanation for Webservice: Webservice is a new platform for building interoperable, distributed applications. The Webservice platform is a set of standards that define how applications can interoperate on the Web.

You can write Webservice on any platform you like in any language you like, as long as we can query and access these services through the Webservice standard.

No matter what tool your Webservice is written in, what language you write, as long as you use the SOAP protocol to invoke it through HTTP, the overall structure is consistent. Usually, you use your own favorite language (such as VB 6 or vb.net)

To build your Webservice and expose it to WEB customers using SOAP Toolkit or. NET built-in support. As a result, customers on any platform can read their WSDL documents in any language,

To call this Webservice. The customer describes the document according to the WSDL and generates a SOAP request message. Webservice are placed behind a WEB server (such as IIS), a customer-generated SOAP request

will be embedded in an HTTP POST request and sent to the WEB server. The WEB server then forwards these requests to the Webservice request processor. The purpose of the request processor is to parse the received SOAP request,

Call Webservice, and then generate the corresponding SOAP reply.   After the WEB server has received a SOAP response, it is sent back to the client via an HTTP reply. "

The above comes from the network.

----------------------------------------------------------------------

Build CXF and Spring-integrated WebService:

Environment:

Java7

CXF 2.7.13

CXF bag with spring.30 for direct use

Create a new Web project name TEST,CXF all the packages in the Lib file below are copied to the project Lib. Seemingly pack a lot, do not know those are redundant, simply directly with all.

Structure:

Interface

@WebService  Public Interface Outservice {        public string getbinserial (@WebParam (name= "text") string text);    } 

Implementation class

// Add Endpointinterface Access when @WebService (endpointinterface= "com.xxx.xxx") client and service-side package name are different @WebService @component ("Outserviceimpl")  Public class Implements outservice{    public  string getbinserial (string text) {                new  Jsonobject ();        Jo.put ("KK",N);        Jo.put ("ww", "ww");         return jo.tostring ();    }}

The component annotation here is the spring annotation equivalent to the applicationcontext.xml in the

< Bean    = "Outserviceimpl"  class= "Com.xxxx ..."  />


Here's applicationcontext.xml.

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:jaxws= "Http://cxf.apache.org/jaxws"XMLNS:CXF= "Http://cxf.apache.org/core"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0. XSD Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring -context-3.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">                     <Context:annotation-config/>    <Context:component-scanBase-package= "COM.TEK.KS" />              <!--Cxf.xml,cxf-extension-soap.xml,cxf-servlet.xml placed in the CXF directory of the Meta-inf folder inside the Cxf-2.5.3.jar -              <ImportResource= "Classpath:meta-inf/cxf/cxf.xml" />      <ImportResource= "Classpath:meta-inf/cxf/cxf-extension-soap.xml" />      <ImportResource= "Classpath:meta-inf/cxf/cxf-servlet.xml" />          <!--Jax-ws -    <!--implementor Specifies the WebService implementation class, address specifies access addresses -    <Jaxws:endpointID= "HelloWorld"implementor= "#outServiceImpl"Address= "/outservice" />         </Beans>

Web. XML configuration

<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "3.0"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">     <!--Spring Configuration -   <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>     <!--CXF Configuration -     <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>/*</Url-pattern>    </servlet-mapping>          <welcome-file-list>    <Welcome-file>index.jsp</Welcome-file>  </welcome-file-list></Web-app>

Here to build, deploy to Tomcat

Access in the browser address bar: http://localhost:8080/test/outService?wsdl display like that, it means the building was successful.

The above cxf+spring service-side construction completed.

Here is the client,

 public  static  void   main (string[] args) {Jaxwsproxyfactorybean factory  = new   Jaxwsproxyfactorybean ();         //  Register WebService interface  Factory.setserviceclass (Outservice.        Class  );         //  set WebService address         Factory.setaddress ("Http://localhost:8080/test/outService" );        Outservice os  = (Outservice) factory.create ();        String msg  =os.getbinserial ("ssss" );    SYSTEM.OUT.PRINTLN (msg); }  

Output print json:{"KK": "ww": "WW"}

Project requirements to call CXF's WebService using C #

Here's how:

I'm using vs2008, creating a new project, right-clicking on a project-adding a service reference-entering the address in http://localhost:8080/test/outService?wsdl that's the address of the previous test server.

Click to go, in the service will be able to find services. then click OK.

Be careful here. Click to go to find the service, the service can not be turned off, this is not difficult to understand it.

And then you can see

And then it was simple. Code

            Servicereference1.outserviceclient   New  WindowsFormsApplication1.ServiceReference1.OutServiceClient ();             string json = osc.getbinserial ("sss");            Console.WriteLine (JSON);

Print: {"KK": "ww": "WW"}

JSON got it.

Cxf+spring Build WebService

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.