根据前面定义的UserService.xsd文件,我们知道,需要定义的JavaBean有三个,分别是User、UserRequest及UserResponse,这三个Javabean采用的namespace、xml rool、相关属性都已在UserService.xsd当中标明,我们需要做的就是按UserService.xsd文件定义的信息来编写Javabean即可。首先来看看User类,其源码如下:
package ws;
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",namespace="http://www.bstek.com/ws")
@XmlAccessorType(XmlAccessType.FIELD)
public class User{
private String username;
private Date birthday;
private boolean gender;
private String companyId;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public String getCompanyId() {
return companyId;
}
public void setCompanyId(String companyId) {
this.companyId = companyId;
}
}
在这个User类当中,我们添加了两个Annotation,分别是XmlRootElement及XmlAccessorType,其中XmlRootElement用于根据XSD文件中定义的XmlRool及namespace来定义这个User类在序列化成XML之后Xml root值及namespace的值;XmlAccessorType用于定义这个bean中属性在序列化成xml之后子节点名及通过什么样方式访问这些属性值,这里定义成XmlAccessType.FIELD,表示直接采用属性名方式。
UserRequest与UserResponse与User类定义方式相同,全部是严格按照UserService.xsd定义编写,它们的源码如下:
package ws;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="UserRequest",namespace="http://www.bstek.com/ws")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserRequest {
private int userCount;
private String targetCompany;
public int getUserCount() {
return userCount;
}
public void setUserCount(int userCount) {
this.userCount = userCount;
}
public String getTargetCompany() {
return targetCompany;
}
public void setTargetCompany(String targetCompany) {
this.targetCompany = targetCompany;
}
}
package ws;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="UserResponse",namespace="http://www.bstek.com/ws")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserResponse {
private List<User> users;
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
Javabean定义完成之后,接下来就可以定义负责具体接受客户端请求的Webservice服务类啦,在Spring-WS当中,这个类叫做Endpoint,我们来看看如何编写这个Endpoint。