Spring MVC + mybaties + MySQL perfectly integrates CXF implementation WebService interface (server, client)

Source: Internet
Author: User

spring-3.1.2, cxf-3.1.3, mybaties, MySQL integration to implement WebService required full jar file address: http://download.csdn.net/detail/xuxiaoyu__/9868032

The process of integration is really simple, with a simple configuration, but sadly a bunch of problems are caused by missing jar or jar version conflicts, so attach all jar files ↑ ↑

Publish WebService Service :

First step: Writing interfaces and implementing classes

Plcdatadetail.java

1 Package test;2 3 import java.util.Date;4 5 Public class plcdatadetail{6 7 private Integer ID;8 9 private Integer plc_info_address_id;Ten  One private String value; A  - private Date create_time; -  the Public Integer getId () { - return ID; -     } -  + Public void SetId (Integer id) { - this.id = ID; +     } A  at Public Integer getplc_info_address_id () { - return plc_info_address_id; -     } -  - Public void setplc_info_address_id (Integer plc_info_address_id) { - this.plc_info_address_id = plc_info_address_id; in     } -  to Public String GetValue () { + return value; -     } the  * Public void SetValue (String value) { $ this.value = value;Panax Notoginseng     } -  the Public Date Getcreate_time () { + return create_time; A     } the  + Public void Setcreate_time (Date create_time) { - this.create_time = create_time; $     } $}
View Code

Basewebservice.java

1  PackageXD.SERVICE.PLC;2 3 ImportJavax.jws.WebService;4 5 ImportXd.persistence.beans.PlcDataDetail;6 7 /**8 * This class is generated by Apache CXF 3.1.69 * 2017-06-12t10:16:26.245+08:00Ten * Generated Source version:3.1.6 One  *  A  */ - //You must assign a value to targetnamespace, and the value must be the same when another project calls the interface, or it will not match the - //(You can assign the default value to targetnamespace at publish time) the@WebService (targetnamespace = "http://impl.plc.service.xd/") -  Public InterfaceBasewebservice { -  -      Publicplcdatadetail readplcdata (Integer ID); +}

Basewebserviceimp.java

1  PackageXd.service.plc.impl;2 3 ImportJavax.jws.WebService;4 5 Importorg.springframework.beans.factory.annotation.Autowired;6 7 ImportXd.persistence.beans.PlcDataDetail;8 ImportXd.service.plc.BaseWebService;9 ImportXd.service.plc.PlcDataDetailService;Ten  One  A@WebService (endpointinterface= "Xd.service.plc.BaseWebService", servicename= "Plcdataservice") -  Public classBasewebserviceimpImplementsBasewebservice { - @Autowired the     Privateplcdatadetailservice plcdatadetailservice;//Business Processing class -      -      Publicplcdatadetail readplcdata (Integer id) { -Plcdatadetail Plcdatadetail =NewPlcdatadetail (); +Plcdatadetail =Plcdatadetailservice.selectbyid (ID); -         returnPlcdatadetail; +     } A  at}

Step Two: Configure Spring's core file Application-context.xml

Add Namespaces and configure interface Publishing information:

1 2 xmlns:jaxrs= "Http://cxf.apache.org/jaxrs"34 xsi:schemalocation= "/http Cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd5 http://http://cxf. Apache.org/schemas/jaxrs.xsd"6 <!--address: Exposed addresses, name their own random; the id attribute is not added here and there is no problem-7 <jaxws:endpoint implementor= "xd.service.plc.impl.BaseWebServiceImp" address= "/readplcdata"/>

Configure Web. XML:

<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<!--the WebService path to this system must start with/ws/--
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>

Access: Http://localhost:8080/ws to see if the WSDL is displayed (the project is not required to enter the project name through Tomcat configuration, students should note) results :

Client Invoke Interface:

Create a new project, the service interface class Basewebservice.java copied over, Jar also to copy Oh, write Test class:

Basewebservice.java

1  Packagetest;2 3 ImportJavax.jws.WebService;4 5 /**6 * This class is generated by Apache CXF 3.1.67 * 2017-06-12t10:16:26.245+08:008 * Generated Source version:3.1.69  * Ten  */ One@WebService (targetnamespace = "http://impl.plc.service.xd/") A  Public InterfaceBasewebservice { -  -      Publicplcdatadetail readplcdata (Integer ID); the}

Myclient.java

1  Packagetest;2 3 Importjava.util.Properties;4 5 ImportOrg.apache.cxf.jaxws.JaxWsProxyFactoryBean;6 7  Public classmyclient {8      Public Static voidMain (String args[]) {9         //The following three lines of code is the system initialization settings, if it is independent of the service interface out of use, do not add, if the call in the interface project needs to add, otherwise it will be an errorTenProperties props =system.getproperties (); OneProps.setproperty ("Org.apache.cxf.stax.allowInsecureParser", "1");  AProps.setproperty ("Usesunhttphandler", "true"); -         //Create a WebService Client Agent factory -Jaxwsproxyfactorybean factory =NewJaxwsproxyfactorybean (); the         //registering the WebService interface -Factory.setserviceclass (Basewebservice.class); -         //Set WebService Address -Factory.setaddress ("Http://localhost:8080/ws/ReadPlcData"); +Basewebservice Service =(Basewebservice) factory.create (); -System.out.println (Service.readplcdata (14). GetValue ());//parameter query +     } A}

Summary: Online resources are many, there are new resources, but also a long time ago, in the integration of the time there are a lot of conflicts, to pay attention to their projects in the CXF version. For example, just started to follow the online in the spring file to configure the following three lines of resources, the result is always error said can not find, in fact, after the CXF 3.x version does not need to manually configure, so look for the latest problem, old does not necessarily apply, with the old version in the development process may be a big hole ah.

<resource= "Classpath:meta-inf/cxf/cxf.xml"/>  <  resource= "Classpath:meta-inf/cxf/cxf-extension-soap.xml"/>   <resource= "Classpath:meta-inf/cxf/cxf-servlet.xml"/>

Spring MVC + mybaties + MySQL perfectly integrates CXF implementation WebService interface (server, client)

Related Article

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.