2016-03-08 149 views
0

我想返回一個ArrayList的chatbox類對象,其中有一個字段allowedUser是另一個類對象User的ArrayList。然而每當我嘗試返回一個值,它只會返回其他字段。java返回數組列表的一個對象的數組列表另一個對象的其餘對象

我附上了模型類和服務的代碼。你們能幫我找出解決這個問題的最佳方法嗎?提前致謝。

User.java

package com.mycompany.samplehospital.model; 

import java.io.Serializable; 
import java.util.Map; 

import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlTransient; 
import Authentication.HashPassword; 
import com.mycompany.samplehospital.Services.AllServices; 
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* 
* @author sandeshpoudel 
*/ 
@XmlRootElement 

public class User implements Serializable { 

    private static final long serialVersionUID = 1L; 
    private String title; 
    private int age; 
    private String Sex; 
    private String Address; 
    private int phoneNo; 
    private String fullName; 
    private int id; 
    private Map<Integer, Message> allMessage; 
    private Map<Integer,Alert> allAlerts; 
    private String userName; 
    private String passWord; 
    private String Role; 
    private HashPassword hs; 


    private Map<Integer, User> UsersList; 

    public User() { } 

    public User(int userId, String fullName, String Sex, Integer age, Integer phoneNumber, String Address, String title,String userName,String password,String Role) throws Exception { 
     UsersList = AllServices.getUsers(); 
     hs = new HashPassword(); 
     this.id= userId; 

     this.fullName = fullName; 
     this.title = title; 
     this.age = age; 
     this.Sex = Sex; 
     this.Address = Address; 
     this.phoneNo = phoneNumber; 
     this.Role = Role; 
     setPassWord(password); 
     this.userName= userName; 
    } 

    @XmlElement 
    public String getRole() { 
     return Role; 
    } 

    public void setRole(String Role) { 
     this.Role = Role; 
    } 

    public void setId(Integer id){ 
     this.id= id; 
    } 

    public void setFullName(String fullName) { 
     this.fullName = fullName; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public void setAge(int age) { 
     this.age = age; 
    } 

    public void setSex(String Sex) { 
     this.Sex = Sex; 
    } 

    public void setAddress(String Address) { 
     this.Address = Address; 
    } 

    public void setPhoneNo(int phoneNo) { 
     this.phoneNo = phoneNo; 
    } 

    public String getUserName() { 
     return userName; 
    } 

    public void setUserName(String userName) { 
     this.userName = userName; 
    } 

    @XmlElement 
    public String getPassWord() { 
     return passWord; 
    } 

    public void setPassWord(String passWord) throws Exception { 
     hs = new HashPassword(); 
     this.passWord = hs.encrypt(passWord); 
    } 

    @XmlElement 
    public String getFullName() { 
     return fullName; 
    } 

    @XmlElement 
    public int getId(){ 
     return id; 
    } 

    @XmlElement 
    public String getTitle() { 
     return title; 
    } 

    @XmlElement 
    public int getAge() { 
     return age; 
    } 

    @XmlElement 
    public String getSex() { 
     return Sex; 
    } 

    @XmlElement 
    public String getAddress() { 
     return Address; 
    } 

    @XmlElement 
    public int getPhoneNo() { 
     return phoneNo; 
    } 

    @XmlTransient 
    public Map<Integer, Message> getAllMessage() { 
     return allMessage; 
    } 

    public void setAllMessage(Map<Integer, Message> allMessage) { 
     this.allMessage = allMessage; 
    } 

    @XmlTransient 
    public Map<Integer, Alert> getAllAlerts() { 
     return allAlerts; 
    } 

    public void setAllAlerts(Map<Integer, Alert> allAlerts) { 
     this.allAlerts = allAlerts; 
    } 

    @Override 
    public String toString(){ 
     return (getSex() +" "+ getAddress()+" "+ getPhoneNo() +" "+ getFullName()); 
    } 
} 

chatBox.java

package com.mycompany.samplehospital.model; 

import java.util.ArrayList; 
import java.util.List; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 

/** 
* 
* @author sandeshpoudel 
*/ 
@XmlRootElement 
public class ChatBox { 

    private String chatName; 
    private String Password; 
    private Integer chatBoxId; 

    private ArrayList<User> AllowedUser; 

    public ChatBox(Integer id,String chatName, String Password, ArrayList<User> Users) { 
     this.chatName = chatName; 
     this.Password = Password; 
     setChatBoxId(id); 
     this.AllowedUser = Users;   
    } 

    public ChatBox() { } 

    @XmlElement 
    public String getChatName() { 
     return chatName; 
    } 

    public void setChatName(String chatName) { 
     this.chatName = chatName; 
    } 

    @XmlElement 
    public String getPassword() { 
     return Password; 
    } 

    public void setPassword(String Password) { 
     this.Password = Password; 
    } 

    @XmlElement 
    public Integer getChatBoxId() { 
     return chatBoxId; 
    } 

    public void setChatBoxId(Integer id) { 
     this.chatBoxId=id; 
    } 

    @XmlElement 
    public ArrayList<User> getUsers() { 
     return AllowedUser; 
    } 

    public void setUsers(ArrayList<User> Users) { 
     this.AllowedUser = Users; 
    } 
} 

chatBoxServices.java

package com.mycompany.samplehospital.Services; 

import com.mycompany.samplehospital.model.ChatBox; 
import com.mycompany.samplehospital.model.User; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Map; 

/** 
* 
* @author sandeshpoudel 
*/ 
public class ChatBoxServices { 

    private static final long serialVersionUID = 1L; 
    private static final Map<Integer, User> users = AllServices.getUsers(); 
    private static final Map<Integer, ChatBox> chatBoxList = AllServices.getChatBox(); 
    ArrayList<User> userList; 

    public ChatBoxServices() throws Exception { 

     userList= (ArrayList<User>) UserServices.getUsers(); 
     chatBoxList.put(1, new ChatBox(1,"case1", "case1password", userList)); 
     chatBoxList.put(2, new ChatBox(2,"case2", "case2password", userList)); 
    } 

    public static List<ChatBox> getAllChatBOx() { 
     return new ArrayList<>(chatBoxList.values()); 
    } 

    public ChatBox getChatBox(int id) { 
     return chatBoxList.get(id); 
    } 

    public static ChatBox addChatBox(ChatBox chatBox) { 
     Integer size =chatBoxList.size(); 
     chatBox.setChatBoxId(size+1); 

     chatBoxList.put(chatBox.getChatBoxId(), chatBox); 
     return chatBox;  
    } 

    public ChatBox updateChatBox(ChatBox chatBox) { 

     if (chatBox.getChatBoxId() < 1) { 
      return null; 
     } 

     chatBoxList.put(chatBox.getChatBoxId(), chatBox); 
     return chatBox; 
    } 

    public ChatBox removeChatBox(Integer id) { 
     return chatBoxList.remove(id); 
    } 

} 

chatBoxResources.java

package com.mycompany.samplehospital.resources; 

import com.mycompany.samplehospital.Services.ChatBoxServices; 
import com.mycompany.samplehospital.exception.objectNotFound; 
import com.mycompany.samplehospital.model.ChatBox; 
import java.util.List; 
import javax.ws.rs.Consumes; 
import javax.ws.rs.DELETE; 
import javax.ws.rs.GET; 
import javax.ws.rs.POST; 
import javax.ws.rs.PUT; 
import javax.ws.rs.Path; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 

/** 
* 
* @author sandeshpoudel 
*/ 
@Path("/chatbox") 
public class ChatBoxResources { 

    ChatBoxServices chatService; 

    public ChatBoxResources() throws Exception { 
     this.chatService = new ChatBoxServices(); 
    } 

    @GET 
    @Produces(MediaType.APPLICATION_XML) 
    public List<ChatBox> getAllChatBOx() { 
     return ChatBoxServices.getAllChatBOx();  
    } 

    @Path("/{ChatBoxId}") 
    @GET 
    @Produces(MediaType.APPLICATION_XML) 
    public ChatBox getAlert(@PathParam("ChatBoxId") int ID) { 

     ChatBox newChatBOx = chatService.getChatBox(ID); 
     if (newChatBOx == null) { 
      throw new objectNotFound("chat room not Found"); 

     } 
     return newChatBOx; 

    } 

    @POST 
    @Produces(MediaType.APPLICATION_XML) 
    @Consumes(MediaType.APPLICATION_XML)  
    public ChatBox addBox(ChatBox chat) { 
     return ChatBoxServices.addChatBox(chat); 
    } 

    @PUT 
    @Path("/{BoxId}") 
    @Produces(MediaType.APPLICATION_XML) 
    @Consumes(MediaType.APPLICATION_XML)  
    public ChatBox updtaeUser(ChatBox chat) { 
     ChatBox newBox = chatService.getChatBox(chat.getChatBoxId()); 
     if (newBox == null) { 
      throw new objectNotFound(" chat box not Found"); 
     } 

     return chatService.getChatBox(newBox.getChatBoxId()); 

    } 

    @DELETE 
    @Path("/{BOxId}") 
    @Produces(MediaType.APPLICATION_XML) 
    public List<ChatBox> delBOx(@PathParam("BOxId") int ID) { 
     chatService.removeChatBox(ID); 
     return ChatBoxServices.getAllChatBOx(); 
    } 
} 

getAllChatBOx輸出

<chatBoxes> 
    <chatBox> 
    <chatBoxId>1</chatBoxId> 
    <chatName>case1</chatName> 
    <password>case1password</password> 
    </chatBox> 
    <chatBox> 
    <chatBoxId>2</chatBoxId> 
    <chatName>case2</chatName> 
    <password>case2password</password> 
    </chatBox> 
</chatBoxes> 
+0

你能成爲一個更具體一點?你剛剛傾倒的代碼量巨大這裏究竟哪裏是你有問題嗎?哪種方法/類/線? –

+0

當然,我有chatbox類,其中有字段allowedUser這是用戶類對象的arraylist ..這裏的問題是在休息XML數據我有除用戶arraylist的所有其他字段,你可以看到..我希望你現在得到它.. –

+1

你還可以添加'User'類嗎?這對你的問題來說很重要,但它缺失。 –

回答

0

我的猜測是,在客艙情況下,用戶列表爲空或空。

檢索用戶列表的方式很奇怪。您有AllServices和UserSevices類,它們都返回用戶列表,您的User類構造函數調用其中一種方法來獲取用戶列表(即使該用戶類中未使用該列表),這可能會導致遞歸AllServices.getUsers方法試圖創建用戶對象,使用構造函數。需要

一些調試!

相關問題