2016-03-08 159 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方法试图创建用户对象,使用构造函数。需要

一些调试!

相关问题