我有一个 java 网络服务
在其中,我想返回一个我称之为响应的对象
我有几个相同父类型的对象
public class ParentType{
public ParentType();
}
public class Childtype1 extends ParentType{
}
public class Childtype2 extends ParentType{
}
public class Response{
ParentType data;
}
我的问题是,当我返回 childtype1 的对象时,一切都很好
但是当我尝试返回 childtype2 的对象时,数据对象返回为空
我为此使用Axis。
如何使 childtype1 和 childtype2 都返回数据?
编辑
这是实际的代码:
public class ServerObject{
public ServerObject(){
}
//this is really an empty class, nothing written here besides this
}
public User extends ServerObject(){
private int userID = 0;
private String firstName = "N/A";
private String lastName = "N/A";
private String login;
private String password = "N/A";
private int authLevel = 0;
private String address = "N/A";
private String phone = "N/A";
private String email = "N/A";
public User(){
super();
}
public int getUserID() {
return userID;
}
public void setUserID(int userID) {
this.userID = userID;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAuthLevel() {
return authLevel;
}
public void setAuthLevel(int authLevel) {
this.authLevel = authLevel;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [userID=" + userID + ", firstName=" + firstName + ", lastName=" + lastName + ", login=" + login + ", password=" + password + ", authLevel=" + authLevel + ", address=" + address
+ ", phone=" + phone + ", email=" + email + "]";
}
}
public class UserLoginInformation extends ServerObject{
private int userID = 0;
private long lastSeen=System.currentTimeMillis();
private boolean loggedIn = false;
private String login="N/A";
public int getUserID() {
return userID;
}
public boolean isLoggedIn() {
return loggedIn;
}
public void setLoggedIn(boolean loggedIn) {
this.loggedIn = loggedIn;
this.lastSeen = System.currentTimeMillis();
}
public void setLogin(String login) {
this.login = login;
}
public String getLastSeen() {
return lastSeen+"";
}
public String getLogin() {
return login;
}
public void setUserID(int userID) {
this.userID = userID;
}
public void setLastSeen(long lastSeen) {
this.lastSeen = lastSeen;
}
@Override
public String toString() {
return "UserLoginInformation [userID=" + userID + ", lastSeen=" + lastSeen + ", loggedIn=" + loggedIn + ", login=" + login + "]";
}
}
public class ServerResponse{
ServerObject data;
int status;
String message;
public ServerResponse(){
//also empty constructor, does nothing
}
public ServerObject getData(){
Log.info(data);
return data;
}
public int getStatus(){
return status;
}
public String getMessage(){
return message;
}
public void setData(ServerObject data){
this.data = data;
}
public void setStatus(int status){
this.status = status;
}
public void setMessage(String message){
this.message = message;
}
@Override
public String toString{
return "ServerResponse [data="+data+" status = " +status + " message= "+message]";
}
}
================ 自动生成的wsdl
<wsdl:message name="testUserRequest">
<wsdl:part element="impl:testUser" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="testUserLoginInformationRequest">
<wsdl:part element="impl:testUserLoginInformation" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="testUserResponse">
<wsdl:part element="impl:testUserResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="testUserLoginInformationResponse">
<wsdl:part element="impl:testUserLoginInformationResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="testOrderRequest">
<wsdl:part element="impl:testOrder" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="testOrderResponse">
<wsdl:part element="impl:testOrderResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="ElectronicArenaWebService">
<wsdl:operation name="testUserLoginInformation">
<wsdl:input message="impl:testUserLoginInformationRequest" name="testUserLoginInformationRequest">
</wsdl:input>
<wsdl:output message="impl:testUserLoginInformationResponse" name="testUserLoginInformationResponse">
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="testOrder">
<wsdl:input message="impl:testOrderRequest" name="testOrderRequest">
</wsdl:input>
<wsdl:output message="impl:testOrderResponse" name="testOrderResponse">
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="testUser">
<wsdl:input message="impl:testUserRequest" name="testUserRequest">
</wsdl:input>
<wsdl:output message="impl:testUserResponse" name="testUserResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ElectronicArenaWebServiceSoapBinding" type="impl:ElectronicArenaWebService">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="testUserLoginInformation">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="testUserLoginInformationRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="testUserLoginInformationResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="testOrder">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="testOrderRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="testOrderResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="testUser">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="testUserRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="testUserResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ElectronicArenaWebServiceService">
<wsdl:port binding="impl:ElectronicArenaWebServiceSoapBinding" name="ElectronicArenaWebService">
<wsdlsoap:address location="http://localhost:9080/ElectronicArenaLenasProject/services/ElectronicArenaWebService"/>
</wsdl:port>
</wsdl:service>
==================== 方法调用 testUser 的响应 - 它只是创建一个新用户并返回它
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<testUserResponse xmlns="http://webservice.lenabru.com">
<testUserReturn>
<BUILD>3.6.2</BUILD>
<data xmlns:ns1="http://database.com" xsi:type="ns1:User">
<address>N/A</address>
<authLevel>0</authLevel>
<email>N/A</email>
<firstName>N/A</firstName>
<lastName>N/A</lastName>
<login xsi:nil="true" />
<password>N/A</password>
<phone>N/A</phone>
<userID>0</userID>
</data>
<status>0</status>
<message />
</testUserReturn>
</testUserResponse>
</soapenv:Body>
</soapenv:Envelope>
方法调用 testUserLoginInformation 的响应 - 它只是创建一个新的 userLoginInformation 并返回它
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<testUserLoginInformationResponse xmlns="http://webservice.lenabru.com">
<testUserLoginInformationReturn>
<BUILD>3.6.2</BUILD>
**<data />** //<======= Problem is here, data is empty
<status>0</status>
<message />
</testUserLoginInformationReturn>
</testUserLoginInformationResponse>
</soapenv:Body>
</soapenv:Envelope>
如您所见,出于某种原因-数据返回为空-我想弄清楚原因
这是 web 服务内部操作的日志输出 - 清楚地表明数据不应为空:
Sep 03, 2013 10:25:02 AM com.electronicarena.utils.Log debug
INFO: [ServerResponse.java line 28 in method: setData]:setting Data: UserLoginInformation [userID=0, lastSeen=1378193102508, loggedIn=false, login=N/A]
Sep 03, 2013 10:25:02 AM com.electronicarena.utils.Log debug
INFO: [ElectronicArenaWebService.java line 64 in method: testUserLoginInformation]:outgoing response: ServerResponse [data=UserLoginInformation [userID=0, lastSeen=1378193102508, loggedIn=false, login=N/A], statusCode=0, statusMsg=, BUILD=3.6.2]
Sep 03, 2013 10:25:02 AM com.electronicarena.utils.Log debug
INFO: [ServerResponse.java line 23 in method: getData]:UserLoginInformation [userID=0, lastSeen=1378193102508, loggedIn=false, login=N/A]
Sep 03, 2013 10:25:45 AM com.electronicarena.utils.Log debug
INFO: [ServerResponse.java line 28 in method: setData]:setting Data: User [userID=0, firstName=N/A, lastName=N/A, login=null, password=N/A, authLevel=0, address=N/A, phone=N/A, email=N/A]
Sep 03, 2013 10:25:45 AM com.electronicarena.utils.Log debug
INFO: [ElectronicArenaWebService.java line 57 in method: testUser]:outgoing response: ServerResponse [data=User [userID=0, firstName=N/A, lastName=N/A, login=null, password=N/A, authLevel=0, address=N/A, phone=N/A, email=N/A], statusCode=0, statusMsg=, BUILD=3.6.2]
Sep 03, 2013 10:25:45 AM com.electronicarena.utils.Log debug
INFO: [ServerResponse.java line 23 in method: getData]:User [userID=0, firstName=N/A, lastName=N/A, login=null, password=N/A, authLevel=0, address=N/A, phone=N/A, email=N/A]