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
- package com.zcz.cxf.service;
-
-   
-
- import javax.jws.webservice;
-
-
- @WebService
- public interface greetingservice {
- public string greeting (string UserName);
- public string say (string eat);
- //public String user (user user);
- }   
Third, write the service implementation class
[Java]View PlainCopy
- Package Com.zcz.cxf.service.impl;
- Import Javax.jws.WebService;
- Import Com.zcz.cxf.service.GreetingService;
- @WebService
- Public class Greetingserviceimpl implements Greetingservice {
- Public string Greeting (string userName) {
- return "Hello!" "+ userName;
- }
- Public string Say (String eat) {
- return "It's time to eat" +eat;
- }
- }
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
- <? 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/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" />
- </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
- <servlet>
- <servlet-name>cxfservlet</servlet-name>
- <servlet-class>org.apache.cxf.transport.servlet.cxfservlet</servlet-class >
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>cxfservlet</servlet-name>
- <url-pattern>/cxf/*</url-pattern>
- </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
- Import Org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
- Public class Testgreetingservice {
- public static void Main (string[] args) {
- //Create WebService Client Agent factory
- Jaxwsproxyfactorybean factory = new Jaxwsproxyfactorybean ();
- //Register WebService interface
- Factory.setserviceclass (Greetingservice. Class);
- //Set WebService address
- Factory.setaddress ("Http://localhost:8080/springmvcModel/cxf/GreetingService");
- Greetingservice Greetingservice = (greetingservice) factory.create ();
- System.out.println ("Start calling WebService ...");
- System.out.println ("The information returned is:" +greetingservice.say ("rice"));
- }
- }
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