Apache CXF Combat II Integrated sping and Web container

Source: Internet
Author: User

This article link: http://blog.csdn.net/kongxx/article/details/7525481

Apache CXF One of the actual combat Hello World Web Service

Read the book below and see how CXF and Spring are integrated.

1. Create HelloWorld Interface class

Package Com.googlecode.garbagecan.cxfstudy.helloworld;

Import Javax.jws.WebMethod;
Import Javax.jws.WebParam;
Import Javax.jws.WebResult;
Import Javax.jws.WebService;

@WebService Public
interface HelloWorld {
    @WebMethod
    @WebResult string Sayhi (@WebParam string text);
2. Create HelloWorld Implementation class
Package Com.googlecode.garbagecan.cxfstudy.helloworld;

public class Helloworldimpl implements HelloWorld {public

    string Sayhi (string name) {
        string msg = "Hello" + N Ame + "!";
        return msg;
    }
}
3. Modify the Web.xml file

<! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.3//en" "Http://java.sun.com/dtd/web-app_2_3.dt D "> <web-app> <display-name>cxfstudy</display-name> <servlet> <servlet-n Ame>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.cxfservlet</ servlet-class> <load-on-startup>1</load-on-startup> </servlet> &LT;SERVLET-MAPPING&G
        T <servlet-name>cxf</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping > <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listene r-class> </listener> <context-param> <param-name>contextconfiglocation</param -name> <param-value>classpath*:* */spring.xml</param-value> </context-param> </web -app>
4. Create the spring configuration file and place it under the Classpath path

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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 "> <import resource=" classpath:meta-inf/cxf/ Cxf.xml "/> <import resource= classpath:meta-inf/cxf/cxf-extension-soap.xml"/> <import "resource=" Spath:meta-inf/cxf/cxf-servlet.xml "/> <jaxws:endpoint id=" HelloWorld "implementor="  Com.googlecode.garbagecan.cxfstudy.helloworld.HelloWorldImpl "address="/helloworld/> <!--for client test --> <jaxws:client id= "helloworldclient address=" Http://localhost:9000/ws/HelloWorld "serviceclass="
 Com.googlecode.garbagecan.cxfstudy.helloworld.HelloWorld "/> </beans>
5. Create a test class

Package Com.googlecode.garbagecan.cxfstudy.helloworld;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

public class Springclient {public
    static void Main (string[] args) {
        ApplicationContext context = new Classpathxml ApplicationContext ("Spring.xml");
        HelloWorld HelloWorld = (HelloWorld) context.getbean ("Helloworldclient");
        System.out.println (Helloworld.sayhi ("kongxx"));
    }

6. Test

6.1 Start Tomcat first or use Maven's jetty, and visit http://localhost:9000/ws/HelloWorld?wsdl to verify that the Web service has started and is in effect;

6.2 Then run the test class to validate the Web service.








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.