Apache CXF practice 2: Integration of Sping and Web containers

Source: Internet
Author: User

Next, let's take a look at how CXF is integrated with spring.

1. Create a HelloWorld Interface Class

 
 
  1. package com.googlecode.garbagecan.cxfstudy.helloworld;  
  2.  
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebParam;  
  5. import javax.jws.WebResult;  
  6. import javax.jws.WebService;  
  7.  
  8. @WebService 
  9. public interface HelloWorld {  
  10.     @WebMethod 
  11.     @WebResult String sayHi(@WebParam String text);  

2. Create a HelloWorld implementation class

 
 
  1. package com.googlecode.garbagecan.cxfstudy.helloworld;  
  2.  
  3. public class HelloWorldImpl implements HelloWorld {  
  4.  
  5.     public String sayHi(String name) {  
  6.         String msg = "Hello " + name + "!";  
  7.         return msg;  
  8.     }  

3. Modify the web. xml file

 
 
  1. <!DOCTYPE web-app PUBLIC  
  2.  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
  3.  "http://java.sun.com/dtd/web-app_2_3.dtd" >  
  4.  
  5. <web-app>  
  6.  
  7.     <display-name>cxfstudy</display-name>  
  8.  
  9.     <servlet>  
  10.         <servlet-name>cxf</servlet-name>  
  11.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  12.         <load-on-startup>1</load-on-startup>  
  13.     </servlet>  
  14.  
  15.     <servlet-mapping>  
  16.         <servlet-name>cxf</servlet-name>  
  17.         <url-pattern>/ws/*</url-pattern>  
  18.     </servlet-mapping>  
  19.  
  20.     <listener>  
  21.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  22.     </listener>  
  23.       
  24.     <context-param>  
  25.         <param-name>contextConfigLocation</param-name>  
  26.         <param-value>classpath*:**/spring.xml</param-value>  
  27.     </context-param>  
  28.       
  29. </web-app> 

4. Create a spring configuration file and place it in the classpath path.

 
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" 
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd      
  5. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
  6.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  7.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  8.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  9.     <jaxws:endpoint id="helloworld" implementor="com.googlecode.garbagecan.cxfstudy.helloworld.HelloWorldImpl" address="/HelloWorld" />  
  10.       
  11.     <!-- For client test -->  
  12.     <jaxws:client id="helloworldClient" address="http://localhost:9000/ws/HelloWorld" serviceClass="com.googlecode.garbagecan.cxfstudy.helloworld.HelloWorld" />     
  13. </beans>  

5. Create a test class

 
 
  1. package com.googlecode.garbagecan.cxfstudy.helloworld;  
  2.  
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.  
  6. public class SpringClient {  
  7.     public static void main(String[] args) {  
  8.         ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");  
  9.         HelloWorld helloworld = (HelloWorld)context.getBean("helloworldClient");  
  10.         System.out.println(helloworld.sayHi("kongxx"));  
  11.     }  
  12. }  

6. Test

6.1 start tomcat or use jetty of maven and access http: // localhost: 9000/ws/HelloWorld? To verify that the web service has been started and takes effect;

6.2 then run the test class to verify the web service.

Series of articles]

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.