Eclipse+cxf+spring co-developed examples for everyone to moment opportune.
A good way to interact with multiple systems (heterogeneous systems) is to invoke a Web service, this example is based on the Apache organization's CXF, which is not possible in the actual project for the convenience of writing the server and client under the same project, but the client relies on the server's web Service interface, you can export the jar by the way.
Environment:
Eclipse mars.1 Release (4.5.1)
JDK 1.7.0_15
Tomcat 7
CXF 2.1.3
Spring3
Example Project structure diagram:
As shown, all dependent third-party libraries are in Lib, and all the code below is posted.
Ihelloservice.java
Package Bing.server;import javax.jws.webservice;/** * <p> * WebService interface * </p> * * @author Icewee * @date 2012-7-6 * @version 1.0 */@WebServicepublic interface Ihelloservice {public string SayHello (String username); }
Helloserviceimpl.java
Package Bing.server;import javax.jws.webservice;/** * <p> * WebService Implementation class * </p> * * @author Icewee * @dat E 2012-7-6 * @version 1.0 */@WebService (endpointinterface = "Bing.server.IHelloService", ServiceName = "HelloService") public class Helloserviceimpl implements Ihelloservice { @Override public string SayHello (string username) { return "Hello," + username; }}
Helloserviceclient.java
Package Bing.client;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import bing.server.ihelloservice;/** * <p > * WebService Caller-Client * </p> * * @author Icewee * @date 2012-7-6 * @version 1.0 */public Class Helloservicecl ient {public static void Main (string[] args) { ApplicationContext context = new Classpathxmlapplicationcontext ( "Applicationcontext-client.xml"); Ihelloservice HelloService = (ihelloservice) Context.getbean ("Client"); String response = Helloservice.sayhello ("Peter"); SYSTEM.OUT.PRINTLN (response); }}
Applicationcontext-server.xml
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "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/beans5 http://www.springframework.org/schema/beans/spring-beans.xsd6 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">7 <!--* * * * * * * Manually added content: xmlns:jaxws= "Http://cxf.apache.org/jaxws" Http://cxf.apache.org/jaxws8 http://cxf.apache.org/schemas/jaxws.xsd " -9 Ten <ImportResource= "Classpath:meta-inf/cxf/cxf.xml" /> One <ImportResource= "Classpath:meta-inf/cxf/cxf-extension-soap.xml" /> A <ImportResource= "Classpath:meta-inf/cxf/cxf-servlet.xml" /> - - <Jaxws:endpointID= "HelloService"implementor= "Bing.server.HelloServiceImpl" the Address= "/helloservice" /> - - </Beans>
Applicationcontext-client.xml
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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 "> <!--* * * * * * * Manually added content: xmlns:jaxws= "Http://cxf.apache.org/jaxws" Http://cxf.apache.org/jaxws http://cxf.apache.org /schemas/jaxws.xsd " - <ImportResource= "Classpath:meta-inf/cxf/cxf.xml" /> <ImportResource= "Classpath:meta-inf/cxf/cxf-extension-soap.xml" /> <ImportResource= "Classpath:meta-inf/cxf/cxf-servlet.xml" /> <BeanID= "Client"class= "Bing.server.IHelloService"Factory-bean= "Clientfactory"Factory-method= "Create" /> <BeanID= "Clientfactory"class= "Org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> < Propertyname= "ServiceClass"value= "Bing.server.IHelloService" /> < Propertyname= "Address"value= "Http://localhost:8082/CXFDemo/ws/helloService" /> </Bean></Beans>
Xml
<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "3.0"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <Display-name>Cxfdemo</Display-name> <Context-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>Classpath:applicationcontext-server.xml</Param-value> </Context-param> <Listener> <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class> </Listener> <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>/ws/*</Url-pattern> </servlet-mapping> <welcome-file-list> <Welcome-file>index.jsp</Welcome-file> </welcome-file-list></Web-app>
All projects have been configured and can be published to Tomcat, entered in the browser: HTTP://LOCALHOST:8082/CXFDEMO/WS, return
From which we can see our external WebService interface, click on the Blue hyperlink and return
This proves that our web service has been successfully published and can be tested for calls. Run Helloserviceclient, return
Use SOAPUI to run the following results:
Disclaimer: This article reproduced from http://www.blogjava.net/icewee/archive/2012/07/06/382399.html to some of the errors encountered in the improvement, the pro-test can be perfectly run
Cxf+spring+eclipse Concise example of "go"