Web service--cxf+spring Integration

Source: Internet
Author: User
Tags soap wsdl

With the spring framework to implement the CXF release of the SOAP protocol services, the steps are basically the same, the difference is that there are some configuration items, the steps are as follows

1. Service-side

First step : Create a Web project (introduce a jar package)

Step two : Create the SEI interface

Import Javax.jws.WebService; Import Javax.xml.ws.BindingType; Import javax.xml.ws.soap.SOAPBinding, @WebService @bindingtype (soapbinding.soap12http_binding)  Public Interface Weatherinterface {    public  string Queryweather (string cityname);}

Step three : Create an SEI implementation class

 public  class  Weatherinterfaceimpl implements   Weatherinterface {@Override  public  Span style= "color: #000000;" > string Queryweather (string cityname) {System.out.println ( from client ... "+ CityName);  if  ("Beijing" .equals (CityName)) { Span style= "color: #0000ff;"        >return  "Cloudy"  else   { return  "Rain turns to Snow" 

Fourth Step : Configure Spring configuration file,applicationcontext.xml, publish service with <jaxws:server> tag , set 1. service address; 2. set up the service interface;3. Set Service Implementation Class

<?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"Xmlns:jaxrs= "Http://cxf.apache.org/jaxrs"XMLNS:CXF= "Http://cxf.apache.org/core"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/                            Spring-beans.xsd Http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd Http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd HTTP://CXF . Apache.org/core http://cxf.apache.org/schemas/core.xsd ">        <!--<jaxws:server The service that publishes the SOAP protocol for Jaxwsserverfactorybean class encapsulation -    <Jaxws:serverAddress= "/weather"ServiceClass= "Com.zang.ws.cxf.server.WeatherInterface">        <Jaxws:servicebean>            <refBean= "Weatherinterface"/>        </Jaxws:servicebean>                <!--Configuring Interceptors -        <jaxws:ininterceptors>            <refBean= "Inintercepter"/>        </jaxws:ininterceptors>        <jaxws:outinterceptors>            <refBean= "Outintercepter"/>        </jaxws:outinterceptors>    </Jaxws:server>    <!--Configuring the Interceptor's Bean -    <Beanname= "Inintercepter"class= "Org.apache.cxf.interceptor.LoggingInInterceptor"/>    <Beanname= "Outintercepter"class= "Org.apache.cxf.interceptor.LoggingOutInterceptor"/>        <!--Configuring the Service implementation class -    <Beanname= "Weatherinterface"class= "Com.zang.ws.cxf.server.WeatherInterfaceImpl"/>            </Beans>                  

Fifth Step : Configure Web. XML, configure the spring profile address and the loaded listener, configure the CXF Servlet.

<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://xmlns.jcp.org/xml/ns/javaee"xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"ID= "webapp_id"version= "3.1">    <Display-name>Ws_cxf_spring_server</Display-name>    <!--setting up the spring environment -    <Context-param>        <!--Contextconfiglocation is not modifiable. -        <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>    <!--Configuring the servlet for CXF -    <servlet>        <Servlet-name>Cxf</Servlet-name>        <Servlet-class>Org.apache.cxf.transport.servlet.CXFServlet</Servlet-class>    </servlet>    <servlet-mapping>        <Servlet-name>Cxf</Servlet-name>        <Url-pattern>/ws/*</Url-pattern>    </servlet-mapping>    <welcome-file-list>        <Welcome-file>index.jsp</Welcome-file>    </welcome-file-list></Web-app>

Sixth step : deploy to Tomcat and start Tomcat

Seventh Step : Test Service, read the user's manual address: http://localhost:8089/ws_cxf_spring_server/ws/weather?wsdl

if Create the implementation class directly, and you can use e ndpoint Label Publishing service. Steps are as follows

To create an implementation class

@WebService  Public class HelloWorld {    public  string SayHello (string name) {        return "Hello," +  Name;}    }

Prior to the implementation of the Sei interface, the applicationcontext.xml was used to publish the service using the <jaxws:server> tag, and by creating the class directly, The applicationcontext.xml should use the <jaxws:endpoint> tag to publish the service.

<!-- <jaxws:endpoint The service that publishes the SOAP protocol for Endpoint class encapsulation  -    <  address= "/hello"  implementor= "Com.zang.ws.cxf.server.HelloWorld" />    

Restart Tomcat, Access manual http://localhost:8089/ws_cxf_spring_server/ws/hello?wsdl

Project structure

2. Client

First step : Introducing the jar package

Step two : generate the client code Wsdl2java command, see Client implementation

Step three : Configure the Spring configuration file ,applicationcontent.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:jaxws= "Http://cxf.apache.org/jaxws"Xmlns:jaxrs= "Http://cxf.apache.org/jaxrs"XMLNS:CXF= "Http://cxf.apache.org/core"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/                            Spring-beans.xsd Http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd Http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd HTTP://CXF . Apache.org/core http://cxf.apache.org/schemas/core.xsd ">    <!--<jaxws:client implements the client for the Jaxwsproxyfactorybean class encapsulation -    <jaxws:clientID= "Weatherclient"Address= "Http://127.0.0.1:8089/ws_cxf_spring_server/ws/weather"ServiceClass= "Com.zang.cxf.weather.WeatherInterface" /></Beans>                  

Fourth Step : get the service implementation class from the spring up and down files, invoke the Query method, print

ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.zang.cxf.weather.WeatherInterface; Public classweatheclient { Public Static voidMain (string[] args) {//initializing the context of springApplicationContext context =NewClasspathxmlapplicationcontext ("Classpath:applicationContext.xml"); //Call Query methodWeatherinterface weatherinterface = (weatherinterface) context.getbean ("Weatherclient"); String Weather= Weatherinterface.queryweather ("Jinan");    SYSTEM.OUT.PRINTLN (weather); }}

Project structure

Web service--cxf+spring Integration

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.