Examples of using CXF to do WebService integration of existing projects

Source: Internet
Author: User
Tags soap

read a lot of CXF information from the Internet, most of them are separate as a WebService project, the example of providing WebService service on the existing spring project is basically not found.

The example I did was to introduce how to integrate CXF into the existing spring projects, and now only do the simple strings and JavaBean, and then study them in a complex way.

Here's an example: a simple example of CXF

First, the application cxf should be loaded into the project by the required package of the service.

For a spring project that has been set up, the missing rack package in my project is

Cxf-2.4.3.jar, Neethi-3.0.1.jar, Wsdl4j-1.6.2.jar, Xmlschema-core-2.0.1.jar, commons-logging-1.1.1 . jar, Spring series of rack packages

Two, the first is the service interface

[Java]View PlainCopy
    1. package com.zcz.cxf.service;  
    2.   
    3.   
    4.   
    5. import javax.jws.webservice;  
    6.   
    7.   
    8. @WebService    
    9. public interface greetingservice {   
    10.    public string greeting (string  UserName);   
    11.    public string  say (string eat);   
    12.    //public  String user (user user);   
    13. }   

Third, write the service implementation class

[Java]View PlainCopy
  1. Package Com.zcz.cxf.service.impl;
  2. Import Javax.jws.WebService;
  3. Import Com.zcz.cxf.service.GreetingService;
  4. @WebService
  5. Public class Greetingserviceimpl implements Greetingservice {
  6. Public string Greeting (string userName) {
  7. return "Hello!"   "+ userName;
  8. }
  9. Public string Say (String eat) {
  10. return "It's time to eat" +eat;
  11. }
  12. }


Four, configure spring startup, the loaded XML file, according to the following XML on the original basis for the addition of editing, only add the red part of my label can be

[HTML]View PlainCopy
  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:jaxws="Http://cxf.apache.org/jaxws"
  5. xsi:schemalocation= "
  6. Http://www.springframework.org/schema/beans
  7. Http://www.springframework.org/schema/beans/spring-beans.xsd
  8. Http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">
  9. <import resource="Classpath:meta-inf/cxf/cxf.xml" />
  10. <import resource="Classpath:meta-inf/cxf/cxf-extension-soap.xml" />
  11. <import resource="Classpath:meta-inf/cxf/cxf-servlet.xml" />
  12. <jaxws:endpoint id="Greetingservice"
  13. implementor="Com.gary.test.ws.service.impl.GreetingServiceImpl"
  14. address="/greetingservice" />
  15. </Beans>

Xmlns:jaxws=http://cxf.apache.org/jaxws

Http://cxf.apache.org/jaxwshttp://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= "Classpath:meta-inf/cxf/cxf-servlet.xml"/>

<jaxws:endpoint id= "Greetingservice" implementor= "Com.gary.test.ws.service.impl.GreetingServiceImpl" address= " /greetingservice "/>

Five, configure the Web. XML for WebService call, add the following code in Web. xml

[HTML]View PlainCopy
  1. <servlet>
  2. <servlet-name>cxfservlet</servlet-name>
  3. <servlet-class>org.apache.cxf.transport.servlet.cxfservlet</servlet-class >
  4. <load-on-startup>1</load-on-startup>
  5. </servlet>
  6. <servlet-mapping>
  7. <servlet-name>cxfservlet</servlet-name>
  8. <url-pattern>/cxf/*</url-pattern>
  9. </servlet-mapping>
[HTML]View PlainCopy


Six, start the project if you access HTTP://LOCALHOST:8080/SPRINGMVCMODEL/CXF, the content that appears as shown is the CXF-based WebService configuration succeeds



Seven, the client calls to the interface

First, create a new service interface class that is the same as the server side Greetingservice

Second, write the class that invokes the service

[Java]View PlainCopy
  1. Import Org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
  2. Public class Testgreetingservice {
  3. public static void Main (string[] args) {
  4. //Create WebService Client Agent factory
  5. Jaxwsproxyfactorybean factory = new Jaxwsproxyfactorybean ();
  6. //Register WebService interface
  7. Factory.setserviceclass (Greetingservice.     Class);
  8. //Set WebService address
  9. Factory.setaddress ("Http://localhost:8080/springmvcModel/cxf/GreetingService");
  10. Greetingservice Greetingservice = (greetingservice) factory.create ();
  11. System.out.println ("Start calling WebService ...");
  12. System.out.println ("The information returned is:" +greetingservice.say ("rice"));
  13. }
  14. }

Configuration success, after the discovery is actually very simple, although do not understand the principle, but will do the basic application.

Examples of using CXF to do WebService integration of existing projects

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.