Cxf create WS-Essay 1

Source: Internet
Author: User

Project directory

// Cxf2.5.2

// ================================== Server-side programs ================== =

// Interface

// Com. Jimmy. ws. ipersonservice

package com.jimmy.ws;import java.util.List;import javax.jws.WebParam;import javax.jws.WebService;import com.jimmy.pojo.Person;@WebServicepublic interface IPersonService {public List<Person> findAll(@WebParam(name = "arg0") String name);}

// Interface implementation class

// Com. Jimmy. ws. personserviceimp

package com.jimmy.ws;import java.util.ArrayList;import java.util.List;import javax.jws.WebService;import com.jimmy.pojo.Person;@WebService(endpointInterface="com.jimmy.ws.IPersonService",serviceName="person")public class PersonServiceImp implements IPersonService{public List<Person> findAll( String name){ArrayList<Person> persons = new ArrayList<Person>();System.out.println(name);System.out.println("Server return all persons");for(int i=0;i<5;i++){Person p = new Person();p.setAge(i+18);p.setName("my name is Xman-"+i);persons.add(p);}return persons;}}


// Pojo

package com.jimmy.pojo;public class Person {private String name;private int age;public Person(){}public Person(String name, int age){this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}

// Spring configuration file bean. xml

<?xml version="1.0" encoding="UTF-8"?><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-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"    xmlns="http://www.springframework.org/schema/beans" >    <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" />    <bean id="jaxWsServiceFactoryBean"         class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">          <property name="wrapped" value="true" />       </bean>       <jaxws:endpoint        id="serviceimp"        address="/person"        implementor="com.jimmy.ws.PersonServiceImp">       <jaxws:serviceFactory>        <ref bean="jaxWsServiceFactoryBean"/>       </jaxws:serviceFactory>    </jaxws:endpoint></beans>

// Web. xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"><web-app id="WebApp_ID">    <display-name>cxfTest</display-name>    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>WEB-INF/beans.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></web-app>

// At this point, the server-side application has been generated

// Deploy it to Tomcat run

// The corresponding WSDL address is http: // localhost: 8080/test/WS/person? WSDL

// Open the WSDL address in the browser and download it. Note that the suffix is changed to WSDL. Here is person. WSDL.


// ================================== Client program ================ ===
// Enter the command line-> cxf installation directory/bin

// Enter the wsdl2java-Client [WSDL installation path]/person. WSDL

// The client structure will be automatically created for you. My structure is as follows:



// Create a Java project in eclipse and copy the directory code generated above to the SRC project, it is best to test the WSDL to the same level of the project Java file (my absolute path on the outside seems problematic)


// After the copy is complete, if the following code in the person_service.java file reports an error, comment out

public Person_Service(WebServiceFeature ... features) {        super(WSDL_LOCATION, SERVICE, features);    }    //This constructor requires JAX-WS API 2.2. You will need to endorse the 2.2    //API jar or re-run wsdl2java with "-frontend jaxws21" to generate JAX-WS 2.1    //compliant code instead.    public Person_Service(URL wsdlLocation, WebServiceFeature ... features) {        super(wsdlLocation, SERVICE, features);    }    //This constructor requires JAX-WS API 2.2. You will need to endorse the 2.2    //API jar or re-run wsdl2java with "-frontend jaxws21" to generate JAX-WS 2.1    //compliant code instead.    public Person_Service(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {        super(wsdlLocation, serviceName, features);    }

// OK, complete

// Enter ipersonservice_personserviceimpport_client.java run

// If an error is reported, the message can not initialize the default WSDL from... this indicates that your WSDL path is incorrect. This is what I mentioned earlier. "It is best to test the WSDL to the same level of the project Java file (my absolute path seems to be wrong )", after the test, change the WSDL address of all absolute paths in the person_service.java file to person. WSDL: remove the preceding path. If your path is correct and no error is reported, ignore the following information:

// The modified person_service.java File

package com.jimmy.ws;import java.net.MalformedURLException;import java.net.URL;import javax.xml.namespace.QName;import javax.xml.ws.WebEndpoint;import javax.xml.ws.WebServiceClient;import javax.xml.ws.WebServiceFeature;import javax.xml.ws.Service;/** * This class was generated by Apache CXF 2.5.2 * 2012-02-24T17:07:12.431+08:00 * Generated source version: 2.5.2 *  */@WebServiceClient(name = "person",                   wsdlLocation = "person.wsdl",                  targetNamespace = "http://ws.jimmy.com/") public class Person_Service extends Service {    public final static URL WSDL_LOCATION;    public final static QName SERVICE = new QName("http://ws.jimmy.com/", "person");    public final static QName PersonServiceImpPort = new QName("http://ws.jimmy.com/", "PersonServiceImpPort");    static {        URL url = Person_Service.class.getResource("person.wsdl");        if (url == null) {            java.util.logging.Logger.getLogger(Person_Service.class.getName())                .log(java.util.logging.Level.INFO,                      "Can not initialize the default wsdl from {0}", "person.wsdl");        }               WSDL_LOCATION = url;    }    public Person_Service(URL wsdlLocation) {        super(wsdlLocation, SERVICE);    }    public Person_Service(URL wsdlLocation, QName serviceName) {        super(wsdlLocation, serviceName);    }    public Person_Service() {        super(WSDL_LOCATION, SERVICE);    }        //This constructor requires JAX-WS API 2.2. You will need to endorse the 2.2    //API jar or re-run wsdl2java with "-frontend jaxws21" to generate JAX-WS 2.1    //compliant code instead.   /* public Person_Service(WebServiceFeature ... features) {        super(WSDL_LOCATION, SERVICE, features);    }    //This constructor requires JAX-WS API 2.2. You will need to endorse the 2.2    //API jar or re-run wsdl2java with "-frontend jaxws21" to generate JAX-WS 2.1    //compliant code instead.    public Person_Service(URL wsdlLocation, WebServiceFeature ... features) {        super(wsdlLocation, SERVICE, features);    }    //This constructor requires JAX-WS API 2.2. You will need to endorse the 2.2    //API jar or re-run wsdl2java with "-frontend jaxws21" to generate JAX-WS 2.1    //compliant code instead.    public Person_Service(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {        super(wsdlLocation, serviceName, features);    }*/    /**     *     * @return     *     returns IPersonService     */    @WebEndpoint(name = "PersonServiceImpPort")    public IPersonService getPersonServiceImpPort() {        return super.getPort(PersonServiceImpPort, IPersonService.class);    }    /**     *      * @param features     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.     * @return     *     returns IPersonService     */    @WebEndpoint(name = "PersonServiceImpPort")    public IPersonService getPersonServiceImpPort(WebServiceFeature... features) {        return super.getPort(PersonServiceImpPort, IPersonService.class, features);    }}

QQ: 390887309. Welcome :)



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.