cxf实现webservice简单插入,查询,和返回结构集

来源:互联网 发布:less.js视频教程 编辑:程序博客网 时间:2024/06/02 10:54

代码如下

User 为实体类

package Test.hw.server;

import java.util.Date;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="User")
@XmlAccessorType(XmlAccessType.FIELD)
public class User {
    private Integer id;
    private String username;
    private String userpwd;
    private String adderss;
    private Date birthday;
    
    public Integer getId() {
        return id;
    }
    public String getUsername() {
        return username;
    }
    public String getUserpwd() {
        return userpwd;
    }
    public String getAdderss() {
        return adderss;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public void setUserpwd(String userpwd) {
        this.userpwd = userpwd;
    }
    public void setAdderss(String adderss) {
        this.adderss = adderss;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    
    
}


Map和list数据在webservice中不能直接使用,必须对Map类型做转换的类和适配器类


package Test.hw.server;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlType(name="UserMap")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserMap {
    @XmlElement(nillable = false, name = "entry")
    List<UserEntry> entries =new ArrayList<UserEntry>();
    
    public List<UserEntry> getEntries() {
        return entries;
    }
    
    @XmlType(name="IdentifiedUser")
    @XmlAccessorType(XmlAccessType.FIELD)
    static class UserEntry{
        @XmlElement(required = true, nillable = false)
        int id;
        User user;
        public int getId() {
            return id;
        }
        public User getUser() {
            return user;
        }
        public void setId(int id) {
            this.id = id;
        }
        public void setUser(User user) {
            this.user = user;
        }
    }

}

package Test.hw.server;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.xml.bind.annotation.adapters.XmlAdapter;





public class UserMapAdapter extends XmlAdapter<UserMap, Map<Integer, User>> {

    @Override
    public UserMap marshal(Map<Integer, User> v) throws Exception {
        // TODO Auto-generated method stub
        UserMap usermap=new UserMap();
        for(Map.Entry<Integer, User> e:v.entrySet()){
            UserMap.UserEntry entry=new UserMap.UserEntry();
            entry.setId(e.getKey());
            entry.setUser((Test.hw.server.User) e.getValue());
            
            usermap.getEntries().add(entry);
        }
        return usermap;
    }

    @Override
    public Map<Integer, User> unmarshal(UserMap v) throws Exception {
        // TODO Auto-generated method stub
        Map<Integer, User> map = new LinkedHashMap<Integer, User>();
        for (UserMap.UserEntry e : v.getEntries()) {
            map.put(e.getId(), (User) e.getUser());
        }
        return map;
    }

}

webService接口和实现

package Test.hw.server;

import java.util.Map;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;


@WebService
public interface UserService {
    @WebResult(name="mothod")
    String insert(User user);
    
    
    User search(@WebParam(name="username",header = true) String username);
    
    @XmlJavaTypeAdapter(UserMapAdapter.class)
    Map<Integer, User> getUsers();
}

package Test.hw.server;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.jws.WebService;

@WebService(endpointInterface = "Test.hw.server.UserService",
        serviceName = "UserService")
public class UserServiceImpl implements UserService {
    Map<Integer, User> users = new LinkedHashMap<Integer, User>();
    
    @Override
    public Map<Integer, User> getUsers() {
        // TODO Auto-generated method stub
        return users;
    }

    @Override
    public String insert(User user) {
        // TODO Auto-generated method stub
        String access="true";
        try {
            users.put(users.size() + 1, user);
        } catch (Exception e) {
            // TODO: handle exception
            access="false";
        }        
        return access;
    }

    @Override
    public User search(String username) {
        // TODO Auto-generated method stub
        User user=null;
        for(Entry<Integer, User> e:users.entrySet()){            
            String name=e.getValue().getUsername();
            if(name.equals(username)){
                user=e.getValue();
            }
        }
        return user;
    }

}

生成wsdl文件

package Test.hw.server;



import javax.xml.ws.Endpoint;

public class Server {

    protected Server() throws Exception {
        // START SNIPPET: publish
        System.out.println("Starting Server");
        UserService implementor = new UserServiceImpl();
        String address = "http://localhost:9000/userService";
        Endpoint.publish(address, implementor);
        // END SNIPPET: publish
    }

    public static void main(String args[]) throws Exception {
        new Server();
        System.out.println("Server ready...");

        Thread.sleep(5 * 60 * 1000);
        System.out.println("Server exiting");
        System.exit(0);
    }
}


使用wsdl2java 生成客户端

wsdl2java -p Test.hw.client -d ./src http://localhost:9090/userService?wsdl


生成如下类





package Test.hw.client;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>getUsers complex type的 Java 类。
 *
 * <p>以下模式片段指定包含在此类中的预期内容。
 *
 * <pre>
 * &lt;complexType name="getUsers">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getUsers")
public class GetUsers {


}


package Test.hw.client;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>getUsersResponse complex type的 Java 类。
 *
 * <p>以下模式片段指定包含在此类中的预期内容。
 *
 * <pre>
 * &lt;complexType name="getUsersResponse">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="return" type="{http://server.hw.Test/}UserMap" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getUsersResponse", propOrder = {
    "_return"
})
public class GetUsersResponse {

    @XmlElement(name = "return")
    protected UserMap _return;

    /**
     * 获取return属性的值。
     *
     * @return
     *     possible object is
     *     {@link UserMap }
     *     
     */
    public UserMap getReturn() {
        return _return;
    }

    /**
     * 设置return属性的值。
     *
     * @param value
     *     allowed object is
     *     {@link UserMap }
     *     
     */
    public void setReturn(UserMap value) {
        this._return = value;
    }

}



package Test.hw.client;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>IdentifiedUser complex type的 Java 类。
 *
 * <p>以下模式片段指定包含在此类中的预期内容。
 *
 * <pre>
 * &lt;complexType name="IdentifiedUser">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="id" type="{http://www.w3.org/2001/XMLSchema}int"/>
 *         &lt;element name="user" type="{http://server.hw.Test/}user" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "IdentifiedUser", propOrder = {
    "id",
    "user"
})
public class IdentifiedUser {

    protected int id;
    protected User user;

    /**
     * 获取id属性的值。
     *
     */
    public int getId() {
        return id;
    }

    /**
     * 设置id属性的值。
     *
     */
    public void setId(int value) {
        this.id = value;
    }

    /**
     * 获取user属性的值。
     *
     * @return
     *     possible object is
     *     {@link User }
     *     
     */
    public User getUser() {
        return user;
    }

    /**
     * 设置user属性的值。
     *
     * @param value
     *     allowed object is
     *     {@link User }
     *     
     */
    public void setUser(User value) {
        this.user = value;
    }

}


package Test.hw.client;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>insert complex type的 Java 类。
 *
 * <p>以下模式片段指定包含在此类中的预期内容。
 *
 * <pre>
 * &lt;complexType name="insert">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="arg0" type="{http://server.hw.Test/}user" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "insert", propOrder = {
    "arg0"
})
public class Insert {

    protected User arg0;

    /**
     * 获取arg0属性的值。
     *
     * @return
     *     possible object is
     *     {@link User }
     *     
     */
    public User getArg0() {
        return arg0;
    }

    /**
     * 设置arg0属性的值。
     *
     * @param value
     *     allowed object is
     *     {@link User }
     *     
     */
    public void setArg0(User value) {
        this.arg0 = value;
    }

}



package Test.hw.client;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>insertResponse complex type的 Java 类。
 *
 * <p>以下模式片段指定包含在此类中的预期内容。
 *
 * <pre>
 * &lt;complexType name="insertResponse">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="mothod" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "insertResponse", propOrder = {
    "mothod"
})
public class InsertResponse {

    protected String mothod;

    /**
     * 获取mothod属性的值。
     *
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getMothod() {
        return mothod;
    }

    /**
     * 设置mothod属性的值。
     *
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setMothod(String value) {
        this.mothod = value;
    }

}


package Test.hw.client;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;


/**
 * This object contains factory methods for each
 * Java content interface and Java element interface
 * generated in the Test.hw.client package.
 * <p>An ObjectFactory allows you to programatically
 * construct new instances of the Java representation
 * for XML content. The Java representation of XML
 * content can consist of schema derived interfaces
 * and classes representing the binding of schema
 * type definitions, element declarations and model
 * groups.  Factory methods for each of these are
 * provided in this class.
 *
 */
@XmlRegistry
public class ObjectFactory {

    private final static QName _GetUsers_QNAME = new QName("http://server.hw.Test/", "getUsers");
    private final static QName _Insert_QNAME = new QName("http://server.hw.Test/", "insert");
    private final static QName _InsertResponse_QNAME = new QName("http://server.hw.Test/", "insertResponse");
    private final static QName _Search_QNAME = new QName("http://server.hw.Test/", "search");
    private final static QName _SearchResponse_QNAME = new QName("http://server.hw.Test/", "searchResponse");
    private final static QName _GetUsersResponse_QNAME = new QName("http://server.hw.Test/", "getUsersResponse");
    private final static QName _Username_QNAME = new QName("http://server.hw.Test/", "username");
    private final static QName _User_QNAME = new QName("http://server.hw.Test/", "User");

    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: Test.hw.client
     *
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link User }
     *
     */
    public User createUser() {
        return new User();
    }

    /**
     * Create an instance of {@link GetUsersResponse }
     *
     */
    public GetUsersResponse createGetUsersResponse() {
        return new GetUsersResponse();
    }

    /**
     * Create an instance of {@link SearchResponse }
     *
     */
    public SearchResponse createSearchResponse() {
        return new SearchResponse();
    }

    /**
     * Create an instance of {@link Search }
     *
     */
    public Search createSearch() {
        return new Search();
    }

    /**
     * Create an instance of {@link InsertResponse }
     *
     */
    public InsertResponse createInsertResponse() {
        return new InsertResponse();
    }

    /**
     * Create an instance of {@link Insert }
     *
     */
    public Insert createInsert() {
        return new Insert();
    }

    /**
     * Create an instance of {@link GetUsers }
     *
     */
    public GetUsers createGetUsers() {
        return new GetUsers();
    }

    /**
     * Create an instance of {@link IdentifiedUser }
     *
     */
    public IdentifiedUser createIdentifiedUser() {
        return new IdentifiedUser();
    }

    /**
     * Create an instance of {@link UserMap }
     *
     */
    public UserMap createUserMap() {
        return new UserMap();
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link GetUsers }{@code >}}
     *
     */
    @XmlElementDecl(namespace = "http://server.hw.Test/", name = "getUsers")
    public JAXBElement<GetUsers> createGetUsers(GetUsers value) {
        return new JAXBElement<GetUsers>(_GetUsers_QNAME, GetUsers.class, null, value);
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link Insert }{@code >}}
     *
     */
    @XmlElementDecl(namespace = "http://server.hw.Test/", name = "insert")
    public JAXBElement<Insert> createInsert(Insert value) {
        return new JAXBElement<Insert>(_Insert_QNAME, Insert.class, null, value);
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link InsertResponse }{@code >}}
     *
     */
    @XmlElementDecl(namespace = "http://server.hw.Test/", name = "insertResponse")
    public JAXBElement<InsertResponse> createInsertResponse(InsertResponse value) {
        return new JAXBElement<InsertResponse>(_InsertResponse_QNAME, InsertResponse.class, null, value);
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link Search }{@code >}}
     *
     */
    @XmlElementDecl(namespace = "http://server.hw.Test/", name = "search")
    public JAXBElement<Search> createSearch(Search value) {
        return new JAXBElement<Search>(_Search_QNAME, Search.class, null, value);
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link SearchResponse }{@code >}}
     *
     */
    @XmlElementDecl(namespace = "http://server.hw.Test/", name = "searchResponse")
    public JAXBElement<SearchResponse> createSearchResponse(SearchResponse value) {
        return new JAXBElement<SearchResponse>(_SearchResponse_QNAME, SearchResponse.class, null, value);
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link GetUsersResponse }{@code >}}
     *
     */
    @XmlElementDecl(namespace = "http://server.hw.Test/", name = "getUsersResponse")
    public JAXBElement<GetUsersResponse> createGetUsersResponse(GetUsersResponse value) {
        return new JAXBElement<GetUsersResponse>(_GetUsersResponse_QNAME, GetUsersResponse.class, null, value);
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link String }{@code >}}
     *
     */
    @XmlElementDecl(namespace = "http://server.hw.Test/", name = "username")
    public JAXBElement<String> createUsername(String value) {
        return new JAXBElement<String>(_Username_QNAME, String.class, null, value);
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link User }{@code >}}
     *
     */
    @XmlElementDecl(namespace = "http://server.hw.Test/", name = "User")
    public JAXBElement<User> createUser(User value) {
        return new JAXBElement<User>(_User_QNAME, User.class, null, value);
    }

}


@javax.xml.bind.annotation.XmlSchema(namespace = "http://server.hw.Test/")
package Test.hw.client;




package Test.hw.client;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>search complex type的 Java 类。
 *
 * <p>以下模式片段指定包含在此类中的预期内容。
 *
 * <pre>
 * &lt;complexType name="search">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "search")
public class Search {


}



package Test.hw.client;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>searchResponse complex type的 Java 类。
 *
 * <p>以下模式片段指定包含在此类中的预期内容。
 *
 * <pre>
 * &lt;complexType name="searchResponse">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="return" type="{http://server.hw.Test/}user" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "searchResponse", propOrder = {
    "_return"
})
public class SearchResponse {

    @XmlElement(name = "return")
    protected User _return;

    /**
     * 获取return属性的值。
     *
     * @return
     *     possible object is
     *     {@link User }
     *     
     */
    public User getReturn() {
        return _return;
    }

    /**
     * 设置return属性的值。
     *
     * @param value
     *     allowed object is
     *     {@link User }
     *     
     */
    public void setReturn(User value) {
        this._return = value;
    }

}


package Test.hw.client;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.datatype.XMLGregorianCalendar;


/**
 * <p>user complex type的 Java 类。
 *
 * <p>以下模式片段指定包含在此类中的预期内容。
 *
 * <pre>
 * &lt;complexType name="user">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="id" type="{http://www.w3.org/2001/XMLSchema}int" minOccurs="0"/>
 *         &lt;element name="username" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         &lt;element name="userpwd" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         &lt;element name="adderss" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         &lt;element name="birthday" type="{http://www.w3.org/2001/XMLSchema}dateTime" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "user", propOrder = {
    "id",
    "username",
    "userpwd",
    "adderss",
    "birthday"
})
public class User {

    protected Integer id;
    protected String username;
    protected String userpwd;
    protected String adderss;
    @XmlSchemaType(name = "dateTime")
    protected XMLGregorianCalendar birthday;

    /**
     * 获取id属性的值。
     *
     * @return
     *     possible object is
     *     {@link Integer }
     *     
     */
    public Integer getId() {
        return id;
    }

    /**
     * 设置id属性的值。
     *
     * @param value
     *     allowed object is
     *     {@link Integer }
     *     
     */
    public void setId(Integer value) {
        this.id = value;
    }

    /**
     * 获取username属性的值。
     *
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getUsername() {
        return username;
    }

    /**
     * 设置username属性的值。
     *
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setUsername(String value) {
        this.username = value;
    }

    /**
     * 获取userpwd属性的值。
     *
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getUserpwd() {
        return userpwd;
    }

    /**
     * 设置userpwd属性的值。
     *
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setUserpwd(String value) {
        this.userpwd = value;
    }

    /**
     * 获取adderss属性的值。
     *
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getAdderss() {
        return adderss;
    }

    /**
     * 设置adderss属性的值。
     *
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setAdderss(String value) {
        this.adderss = value;
    }

    /**
     * 获取birthday属性的值。
     *
     * @return
     *     possible object is
     *     {@link XMLGregorianCalendar }
     *     
     */
    public XMLGregorianCalendar getBirthday() {
        return birthday;
    }

    /**
     * 设置birthday属性的值。
     *
     * @param value
     *     allowed object is
     *     {@link XMLGregorianCalendar }
     *     
     */
    public void setBirthday(XMLGregorianCalendar value) {
        this.birthday = value;
    }

}



package Test.hw.client;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>UserMap complex type的 Java 类。
 *
 * <p>以下模式片段指定包含在此类中的预期内容。
 *
 * <pre>
 * &lt;complexType name="UserMap">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="entry" type="{http://server.hw.Test/}IdentifiedUser" maxOccurs="unbounded" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "UserMap", propOrder = {
    "entry"
})
public class UserMap {

    protected List<IdentifiedUser> entry;

    /**
     * Gets the value of the entry property.
     *
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the entry property.
     *
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getEntry().add(newItem);
     * </pre>
     *
     *
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link IdentifiedUser }
     *
     *
     */
    public List<IdentifiedUser> getEntry() {
        if (entry == null) {
            entry = new ArrayList<IdentifiedUser>();
        }
        return this.entry;
    }

}

package Test.hw.client;

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.6.0
 * 2012-06-11T10:01:41.953+08:00
 * Generated source version: 2.6.0
 *
 */
@WebServiceClient(name = "UserService",
                  wsdlLocation = "http://localhost:9000/userService?wsdl",
                  targetNamespace = "http://server.hw.Test/")
public class UserService_Service extends Service {

    public final static URL WSDL_LOCATION;

    public final static QName SERVICE = new QName("http://server.hw.Test/", "UserService");
    public final static QName UserServiceImplPort = new QName("http://server.hw.Test/", "UserServiceImplPort");
    static {
        URL url = null;
        try {
            url = new URL("http://localhost:9000/userService?wsdl");
        } catch (MalformedURLException e) {
            java.util.logging.Logger.getLogger(UserService_Service.class.getName())
                .log(java.util.logging.Level.INFO,
                     "Can not initialize the default wsdl from {0}", "http://localhost:9000/userService?wsdl");
        }
        WSDL_LOCATION = url;
    }

    public UserService_Service(URL wsdlLocation) {
        super(wsdlLocation, SERVICE);
    }

    public UserService_Service(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public UserService_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 UserService_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 UserService_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 UserService_Service(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {
//        super(wsdlLocation, serviceName, features);
//    }

    /**
     *
     * @return
     *     returns UserService
     */
    @WebEndpoint(name = "UserServiceImplPort")
    public UserService getUserServiceImplPort() {
        return super.getPort(UserServiceImplPort, UserService.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 UserService
     */
    @WebEndpoint(name = "UserServiceImplPort")
    public UserService getUserServiceImplPort(WebServiceFeature... features) {
        return super.getPort(UserServiceImplPort, UserService.class, features);
    }

}


package Test.hw.client;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

/**
 * This class was generated by Apache CXF 2.6.0
 * 2012-06-11T10:01:41.875+08:00
 * Generated source version: 2.6.0
 *
 */
@WebService(targetNamespace = "http://server.hw.Test/", name = "UserService")
@XmlSeeAlso({ObjectFactory.class})
public interface UserService {

    @SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
    @WebResult(name = "searchResponse", targetNamespace = "http://server.hw.Test/", partName = "parameters")
    @WebMethod
    public SearchResponse search(
        @WebParam(partName = "parameters", name = "search", targetNamespace = "http://server.hw.Test/")
        Search parameters,
        @WebParam(partName = "username", name = "username", targetNamespace = "http://server.hw.Test/", header = true)
        java.lang.String username
    );

    @WebResult(name = "mothod", targetNamespace = "")
    @RequestWrapper(localName = "insert", targetNamespace = "http://server.hw.Test/", className = "Test.hw.client.Insert")
    @WebMethod
    @ResponseWrapper(localName = "insertResponse", targetNamespace = "http://server.hw.Test/", className = "Test.hw.client.InsertResponse")
    public java.lang.String insert(
        @WebParam(name = "arg0", targetNamespace = "")
        Test.hw.client.User arg0
    );

    @WebResult(name = "return", targetNamespace = "")
    @RequestWrapper(localName = "getUsers", targetNamespace = "http://server.hw.Test/", className = "Test.hw.client.GetUsers")
    @WebMethod
    @ResponseWrapper(localName = "getUsersResponse", targetNamespace = "http://server.hw.Test/", className = "Test.hw.client.GetUsersResponse")
    public Test.hw.client.UserMap getUsers();
}


最后客户端调用webservice,代码如下
package Test.hw.client;


import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;


import com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl;



public final class Client {

    private static final QName SERVICE_NAME
        = new QName("http://server.hw.Test/", "UserService");
    private static final QName PORT_NAME
        = new QName("http://server.hw.Test/", "UserServiceImplPort");


    private Client() {
    }

    public static void main(String args[]) throws Exception {
        Service service = Service.create(SERVICE_NAME);
        // Endpoint Address
        String endpointAddress = "http://localhost:9000/userService";
        // If web service deployed on Tomcat deployment, endpoint should be changed to:
        // http://localhost:8080/java_first_jaxws-<cxf-version>/services/hello_world

        // Add a port to the Service
        service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
        
     // 标准的JAX-WS 的API
        QName qName = new QName("http://server.hw.Test/","UserService");
        URL url=null;
        try {
            url = new URL("http://localhost:9000/userService?wsdl");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        UserService_Service userImpl=new UserService_Service(url,qName);
        UserService hw=userImpl.getPort(UserService.class);

        User user = new User();
        user.setId(1);
        user.setUsername("admin");
        user.setUserpwd("111111");
        GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar
        .getInstance();
        calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse("1989-01-28"));
        user.setBirthday(new XMLGregorianCalendarImpl(calendar));
                
        System.out.println(hw.insert(user));

        //say hi to some more users to fill up the map a bit
        user = new User();
        user.setId(2);
        user.setUsername("Galaxy");
        user.setUserpwd("888");        
        calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse("1978-02-06"));
        user.setBirthday(new XMLGregorianCalendarImpl(calendar));
        
        System.out.println(hw.insert(user));


        user = new User();
        user.setId(3);
        user.setUsername("Universe");
        user.setUserpwd("123456");
        calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse("2000-02-06"));
        user.setBirthday(new XMLGregorianCalendarImpl(calendar));
        
        System.out.println(hw.insert(user));
        System.out.println("**********************************");
        Search search=new Search();
        SearchResponse user2=hw.search(search, "Universe");
        System.out.println(user2.getReturn().getUsername());
        System.out.println("**********************************");
        System.out.println("Users: ");
        UserMap users = hw.getUsers();
        for (IdentifiedUser name : users.getEntry()) {
            System.out.println("  " + name.getId() + ": " + name.getUser().getUsername());
        }

    }

}









原创粉丝点击