Cxf+spring+tomcat Concise Example

Source: Internet
Author: User
Tags soap

Original source: http://www.blogjava.net/icewee/archive/2012/07/06/382399.html

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:
MyEclipse10
JDK6
Tomcat7
CXF2.5
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; /**  @author@version  1.0*/@WebServicepublic  Interface  ihelloservice {    public  string SayHello (string username);   }


Helloserviceimpl.java

 PackageBing.server;ImportJavax.jws.WebService;/*** <p>* WebService Implementation class * </p>**@authoricewee* @date 2012-7-6*@version1.0*/@WebService (Endpointinterface= "Bing.server.IHelloService", ServiceName = "HelloService") Public classHelloserviceimplImplementsIhelloservice {@Override Publicstring SayHello (string username) {return"Hello," +username; }}

Helloserviceclient.java

 Packagebing.client;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportBing.server.IHelloService;/*** <p>* WebService Caller-client * </p>**@authoricewee* @date 2012-7-6*@version1.0*/ Public classhelloserviceclient { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Applicationcontext-client.xml"); Ihelloservice HelloService= (Ihelloservice) Context.getbean ("Client")); String Response= Helloservice.sayhello ("Peter");    SYSTEM.OUT.PRINTLN (response); }}

Applicationcontext-server.xml

<?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.xsdhttp//Cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd"><!--* * * NOTE * * *What to add manually: Xmlns:jaxws= "Http://cxf.apache.org/jaxws"http://Cxf.apache.org/jaxwshttp://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"/> <jaxws:endpoint id= "HelloService" implementor= " Bing.server.HelloServiceImpl "address="/helloservice "/> </beans>

Applicationcontext-client.xml

<?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.xsdhttp//Cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd"><!--* * * NOTE * * *What to add manually: Xmlns:jaxws= "Http://cxf.apache.org/jaxws"http://Cxf.apache.org/jaxwshttp://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"/> <bean id= "Client"class= "Bing.server.IHelloService" factory-bean= "clientfactory" factory-method= "create"/> <bean id= "Clientfactory"class= "Org.apache.cxf.jaxws.JaxWsProxyFactoryBean" > <property name= "serviceclass" value= "  Bing.server.IHelloService "/> <property name=" Address "value=" Http://localhost:8080/CXFDemo/ws/helloService " /> </bean></beans>


Xml

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "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> <display-name>cx Fservlet</display-name> <servlet-class>org.apache.cxf.transport.servlet.cxfservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name&gt ; 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:8080/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

Add:

1. Start the times wrong, class not fount

Workaround: The jar package for the spring jar and CXF needs to be loaded. There are resources available to download references online.

2. Test Client Side Error

Java.lang.LinkageError: The JAXB 2.1 API is being loaded from the boot class loader, but this RI (from jar:file:/d:/program%20files/java_jdk_45/lib/endorsed/ Jaxb-impl-2.2.6.jar!/com/sun/xml/bind/v2/model/impl/modelbuilder.class) requires 2.2 API. Use the authorization directory mechanism to place Jaxb-api.jar in the Boot class loader. (See http://java.sun.com/j2se/1.6.0/docs/guide/standards/)

Workaround: The JRE System Library needs to be placed first.

Cxf+spring+tomcat Concise Example (RPM)

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.