Spring, CXF, and SpringCXF

Source: Internet
Author: User

Spring, CXF, and SpringCXF

Recently, Webservice has been used in the project, and the official documents of Spring and CXF integration have been studied. (In fact, the official documents are clear. It is recommended that you read the official documents. They are all very simple English words ), so with the demo below, I believe everyone can understand it.

The main architecture used is Maven + Spring + CXF + IDEA. I will not talk much about it. Let's take a look at the entire project structure.

 

I. project structure

 

Ii. Server

1. We will first compile the server interface HelloWorld. java

Package cn. starlin. springcxf; import javax. jws. webService;/*** defines the service interface ** on. * // @ WebServicepublic interface HelloWorld {public String sayHello (String str );}

2. Define the server interface implementation class HelloWorldImpl

Package cn. starlin. springcxf; import javax. jws. webService;/*** service interface implementation class ** on. */@ WebService (endpointInterface = "cn. starlin. springcxf. helloWorld ") public class HelloWorldImpl implements HelloWorld {public String sayHello (String str) {System. out. println ("syaHello called"); return "Hello" + str ;}}

3. Declare the servers bean and create the cxf-servlet.xml File

<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"> <! -- Managed by Spring --> <bean id = "hello" class = "cn. starlin. springcxf. helloWorldImpl "/> <jaxws: endpoint id =" helloWorld "implementor =" # hello "address ="/HelloWorld "/> </beans>

4. Configure web. xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <display-name>cxf</display-name>  <servlet>    <description>Apache CXF Endpoint</description>    <display-name>cxf</display-name>    <servlet-name>cxf</servlet-name>    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>cxf</servlet-name>    <url-pattern>/*</url-pattern>  </servlet-mapping>  <session-config>    <session-timeout>60</session-timeout>  </session-config></web-app>

5. start Tomcat and access http: // localhost: 8080/SpringCXF/. If the interface appears, the server is successful.

 

Iii. Client

1. Client client-beans.xml

<?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/schema/jaxws.xsd">    <bean id="client" class="cn.starlin.springcxf.HelloWorld"          factory-bean="clientFactory" factory-method="create"/>    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">        <property name="serviceClass" value="cn.starlin.springcxf.HelloWorld"/>        <property name="address" value="http://localhost:8080/SpringCXF/HelloWorld"/>    </bean></beans>

2. client code HelloWorldClien. java

Package cn. starlin. springcxf; import org. springframework. context. support. classPathXmlApplicationContext;/*** client ** on 2016/03/01. */public class HelloWorldClient {public static void main (String args []) throws Exception {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (new String [] {"client-beans.xml "}); helloWorld client = (HelloWorld) context. getBean ("client"); // client must be consistent with the id in the client-beans.xml String response = client. sayHello ("starlin"); System. out. println ("Response:" + response); System. exit (0 );}}

3. Run the following client code.

The server also prints the relevant information.

 

 

Integration completed now !!!!

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.