CXF WebService integrated SPRINGMVC maven Project

Source: Internet
Author: User
Tags webservice annotation wsdl

First Recommended Blog: http://www.cnblogs.com/xdp-gacl/p/4259481.html

http://blog.csdn.net/hu_shengyang/article/details/38384597

CXF is an implementation tool for WebService technology, why use CXF to achieve WebService:

1. Java's WebService implementation itself is a very cost-consuming implementation (XML and Java objects between the server and the client to compare consumption performance);

2. Current Java mainstream WebService applications to CXF, AXIS2-based;

3. Through the network channel understanding, at present the efficiency of CXF is at least 50% higher than the AXIS2;

4. There is also a WebService tool that Metro's efficiency is 10% higher than cxf;

5. CXF's implementation information online can be easily found a large number of metro information relatively few;

6. CXF is already very mature in Java application Implementation, the enterprise is more inclined to use such a mature solution;

For these reasons, I chose cxf to implement WebService.

CXF WebService Integration Springmvc:

1. Web. XML is modified as follows:

<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>/webservice/*</url-pattern>  </servlet-mapping>

2. Quoting CXF

Download the CXF toolkit. After extracting the CXF, put all the jar packages under the CXF Kit Lib under the project Lib. The CXF Toolkit version used here is: apache-cxf-2.7.13, or maven reference:

<!--indicate the CFX version--><properties> <!--spring version number in the properties--&GT;&LT;SPRING.VERSION&GT;4.0.2.RELEASE&L t;/spring.version><!--mybatis version number--><mybatis.version>3.2.6</mybatis.version><!-- log4j log File Management Pack version--><slf4j.version>1.7.7</slf4j.version><log4j.version>1.2.17</ log4j.version><!--WebService--<cxf.version>2.7.13</cxf.version></properties><!--webservice:cxf Dependencies--><dependency> <groupid>org.apache.cxf&lt ;/groupid> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}           </version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> &lt ;/dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactid>c Xf-rt-transports-http-jetty</artifactid> <version>${cxf.version}</version> </dependency&gt  ; <dependency> <groupId>org.apache.cxf</groupId> <artifactid>cxf-rt-ws-security&           lt;/artifactid> <version>${cxf.version}</version> </dependency> <dependency> <groupid>orG.apache.cxf</groupid> <artifactId>cxf-rt-ws-policy</artifactId> &LT;VERSION&GT;${CX f.version}</version> </dependency> <dependency> <groupid>org.apache.cxf</groupid&gt           ; <artifactId>cxf-bundle-jaxrs</artifactId> <version>${cxf.version}</version> </depend ency> <dependency> <groupId>javax.ws.rs</groupId> &LT;ARTIFACTID&GT;JSR311-API&L t;/artifactid> <version>1.1.1</version> </dependency><dependency> <group Id>commons-httpclient</groupid> <artifactId>commons-httpclient</artifactId> <ver     Sion>3.0</version> </dependency><!--Webservice:end of CXF Dependencies--

3, Spring.xml in the configuration

<?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:context= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"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://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ SPRING-TX.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.0.xsdHttp://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= "Orderwebservice" address= "/orderwebservice" implementor= "com.bsit.webservice.impl.Or Derwebserviceimpl "/>      <bean id= "Client" class= "Com.bsit.webservice.OrderWebServiceI" factory-bean= "Clientfactory" Factory-met hod= "Create"/> <bean id= "clientfactory" class= "Org.apache.cxf.jaxws.JaxWsProxyFactoryBean" > <PR Operty name= "ServiceClass" value= "Com.bsit.webservice.OrderWebServiceI"/> <property name= "Address" value= " http://192.168.1.20:8080/order/webservice/orderWebService?wsdl "/> </bean></beans>

4. Interface and Implementation class

Define a interface, use @WebService Annotation Callout interface, use @WebMethod annotation to label all methods defined in the interface, implement classes, implement classes with @webservice annotation annotations, Implement all the methods defined in the interface (note the @webservice (endpointinterface= "interface's package name. Interface Name", servicename= "service name"), as follows:

Interface Package Com.bsit.webservice;import Javax.jws.webmethod;import Javax.jws.webparam;import javax.jws.WebService; Import com.bsit.orders.model.Orders; @WebServicepublic interface Orderwebservicei {// Use the @webmethod annotation callout method in the Webservicei interface @webmethodstring SayHello (@WebParam (name= "name") String name); @WebMethodOrders Getordersbynum (String num);} Implementing the class package Com.bsit.webservice.impl;import Javax.jws.webservice;import Org.springframework.beans.factory.annotation.autowired;import Com.bsit.orders.mapper.ordersmapper;import Com.bsit.orders.model.orders;import Com.bsit.webservice.OrderWebServiceI; @WebService (endpointinterface= " Com.bsit.webservice.OrderWebServiceI ", servicename=" Orderwebservice ") public class Orderwebserviceimpl implements orderwebservicei{@Autowiredprivate ordersmapper ordersmapper; @Overridepublic string SayHello (string name) { System.out.println ("WebService SayHello" + name); return "Hello" + name + ", nice to meet.";} @Overridepublic orders getordersbynum (String num) {orders orders = Ordersmapper.getordersbysalesnum (num); Database operations, Ordersmapper injection




}

5. Simple effect

Start Tomcat,webservice with it, enter in the browser: Http://192.168.1.20:8080/order/webservice,

Indicates that the WebService service has been started. The left is the leak out of the method name of the interface, click on the right of the WSDL link, you can view the specific parameters of the method information: You can also enter: HTTP://192.168.1.20:8080/ORDER/WEBSERVICE/ORDERWEBSERVICE?WSDL, open

6. Testing

One

Package Org.bsit.webservice;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.bsit.orders.model.orders;import Com.bsit.webservice.orderwebservicei;public class Orderwebservice1test {/  * * Client calls WebService method, If the red part is configured in Spring.xml, the call is Factory.getbean ("client"), the interface is coerced to WebService */@Testpublic void Testorderwebservice () { ApplicationContext factory = new Classpathxmlapplicationcontext ("/spring.xml"); Convert ORDERWEBSERVICEIORDERWEBSERVICEI client = (orderwebservicei) Factory.getbean ("Client"); orders orders = new orders ( ); orders = Client.getordersbynum ("Bs20160331-2"); System.out.println (Orders.getid () + "," + orders.getsales_order_num ());}}

Two

Package Org.bsit.webservice;import Org.apache.cxf.jaxws.jaxwsproxyfactorybean;import Org.junit.Before;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.bsit.orders.model.orders;import  Com.bsit.webservice.orderwebservicei;public class Orderwebservice2test {private Orderwebservicei orderWebService;/* * The client calls the WebService method, if the red part is not configured in Spring.xml, then the initialization of Initparam () is called first <jaxws:endpoint id= "Orderwebservice" address= " /orderwebservice "implementor=" Com.bsit.webservice.impl.OrderWebServiceImpl "/> */@Before public void INITP            Aram () {Jaxwsproxyfactorybean Factorybean = new Jaxwsproxyfactorybean ();            Factorybean.setserviceclass (Orderwebservicei.class);                      Factorybean.setaddress ("http://192.168.1.20:8080/order/webservice/orderWebService?wsdl");        Orderwebservice = (orderwebservicei) factorybean.create (); } @Testpublic void TeStorderwebservice () {Orders orders = new orders (); orders = Orderwebservice.getordersbynum ("Bs20160331-2"); System.out.println (Orders.getid () + "," + orders.getsales_order_num ());}}

7. Results:

Console printing: 73ccfffc-e7a3-468e-ba7d-5dc75e67886f, bs20160331-2;

Description WebService Injection Mapper successful, can be database query, simple string example did not do the test.

Of course, you can call webservice either in Java or other programs such as. Net.

CXF WebService integrated SPRINGMVC maven Project

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.