Spring Collection CXF released WebService

Source: Internet
Author: User

Publish the WebService interface via spring

Spring jar Package +CXF jar Java package The following files missing jar packages need to be downloaded by themselves

Project structure

1. Entity class

Package Com.test.entity;public class User {public user (string name, string pwd, string sex) {super (); this.name = Name;this . pwd = Pwd;this.sex = sex;} private string Name;private string Pwd;private string Sex;public string GetName () {return name;} public void SetName (String name) {this.name = name;} Public String getpwd () {return pwd;} public void SetPwd (String pwd) {this.pwd = pwd;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;}}

2. WebService interface

Package Com.test.webservice;import Java.util.list;import Javax.jws.WebService;  Import com.test.entity.user;/** * Declare webService interface * @author Administrator * */@WebService public  interface Iwebservic e {public string say (String str);p ublic string Findlist ();}
3. Interface implementation

Package Com.test.webservice.imp;import Java.util.arraylist;import Java.util.list;import javax.jws.WebService; Import Net.sf.json.jsonarray;import com.test.entity.user;import com.test.webservice.iwebservice;/** * Declares that this class is : Iwebservice Implementation class * @author Administrator * */@WebService (endpointinterface= "Com.test.webservice.IWebservice") public Class Webserviceimpl implements Iwebservice {public String say (String str) {return str;} Public String findlist () {list<user> u=new arraylist<user> (); U.add (New User ("Zhang San", "12343", "male")); U.add (new User ("Nicole", "12343", "female"), U.add (New User ("Pros and Cons", "9900000", "female")); Jsonarray json=jsonarray.fromobject (u); return json.tostring ();}}

4. Cxf-service.xml 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:jaxws= "Http://cxf.apache.org/jaxws" xsi : schemalocation= "Http://www.springframework.org/schema/beans HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPR         Ing-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-se Rvlet.xml "/>--<!--ID Custom ServiceClass Interface full package name address-access-<jaxws:server id=" Webse          Rvice "serviceclass=" Com.test.webservice.IWebservice "address="/webservice "> <jaxws:serviceBean> <!--realization Class--<bean class= "Com.test.webservice.imp.WebServiceImpl"/> </jaxws:serviceb Ean> </jaxwS:server> </beans>   

5. Web. XML configuration

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "id=" webapp_id "version=" 2.5 "> <display-name>cxf_service</ display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </ welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> &L T;param-value>classpath:spring/config/cxf-service.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</ser   vlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app>

6. Client Call interface

Package Com.test.web;import Org.apache.cxf.endpoint.client;import Org.apache.cxf.jaxws.JaxWsClientFactoryBean; Import Org.apache.cxf.jaxws.jaxwsproxyfactorybean;import Org.apache.cxf.jaxws.endpoint.dynamic.jaxwsdynamicclientfactory;import Com.test.webservice.iwebservice;public Class TestWebService {public static void main (string[] args) {//call webservice//Jaxwsproxyfactorybean FA Ctory = new Jaxwsproxyfactorybean ();////Factory.setserviceclass (Iwebservice.class);////factory.setaddress ( "Http://localhost:8080/cxf_service/services/webservice");////iwebservice service = (iwebservice) factory.create (); System.out.println ("############ #Client getuserbyname##############");//System.out.println (Service.say ("        ABC "));    Create Jaxwsdynamicclientfactory instance jaxwsdynamicclientfactory fdc=jaxwsdynamicclientfactory.newinstance ();     Get link Client client= fdc.createclient ("http://localhost:8080/cxf_service/services/webservice?wsdl");    Call Interface Name upload parameter return result object[] obj=null; Object[] Objs=null;try {//Executes the specified method say method name parameter obj = Client.invoke ("Say", "I call Zhang San");//Execute method findlist Method Name Objs = Client.invoke ("F Indlist "); System.out.println (Obj[0].tostring ()); System.out.println (Objs[0].tostring ());}    catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} }}


Spring Collection CXF released WebService

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.