Use Apache cxf to create a Web Service

Source: Internet
Author: User
Tags apm

Official homepage: http://cxf.apache.org/

A detailed sample is provided after the download. Here we will first demonstrate a simple project.

Create a project as shown in:

Client test code: testserviceclient. Java

  1. Package JP. co. APM. client;
  2. Import JP. co. APM. Service. testservice;
  3. Import org. Apache. cxf. frontend. clientproxyfactorybean;
  4. Public class testserviceclient {
  5. Public static void main (string [] ARGs ){
  6. Clientproxyfactorybean factory = new clientproxyfactorybean ();
  7. Factory. setserviceclass (testservice. Class );
  8. Factory. setaddress ("http: // localhost: 8080/apm_cxf/services/test ");
  9. Testservice service = (testservice) Factory. Create ();
  10. System. Out. println (service. sayhello ());
  11. }
  12. }

Testserviceimpl. Java

  1. Package JP. co. APM. Service. impl;
  2. Import JP. co. APM. Service. testservice;
  3. Public class testserviceimpl implements testservice {
  4. Public String sayhello (){
  5. Return "Hello, Shen Bin ";
  6. }
  7. }

Testservice. Java

  1. Package JP. co. APM. Service;
  2. Public interface testservice {
  3. Public String sayhello ();
  4. }

Cxf-servlet.xml

  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"
  4. Xmlns: simple = "http://cxf.apache.org/simple"
  5. Xmlns: Soap = "http://cxf.apache.org/bindings/soap"
  6. Xsi: schemalocation ="
  7. Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  8. Http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
  9. Http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd ">
  10. <Simple: Server id = "testservice" serviceclass = "JP. co. APM. Service. testservice" address = "/test">
  11. <Simple: servicebean>
  12. <Bean class = "JP. co. APM. Service. impl. testserviceimpl"/>
  13. </Simple: servicebean>
  14. </Simple: Server>
  15. </Beans>

Web. xml

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee"
  3. Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
  4. Xsi: schemalocation = "http://java.sun.com/xml/ns/javaee
  5. Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
  6. <Display-Name> APM </display-Name>
  7. <Description> APM </description>
  8. <Servlet>
  9. <Servlet-Name> APM </servlet-Name>
  10. <Servlet-class>
  11. Org. Apache. cxf. Transport. servlet. cxfservlet
  12. </Servlet-class>
  13. <Load-on-startup> 2 </load-on-startup>
  14. </Servlet>
  15. <Servlet-mapping>
  16. <Servlet-Name> APM </servlet-Name>
  17. <URL-pattern>/services/* </url-pattern>
  18. </Servlet-mapping>
  19. <Session-config>
  20. <Session-Timeout> 60 </session-Timeout>
  21. </Session-config>
  22. </Web-app>

Start Tomcat and access http: // localhost: 8080/apm_cxf/services/test? WSDL

Run testserviceclient. Java to test the result.

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.