java返回一个对象的数组列表,其中包含数组列表的另一个对象,用于休息api

时间:2016-03-08 08:21:39

标签: java arrays xml jax-rs

我正在尝试返回chatbox类对象的ArrayList,其具有字段allowedUser,该字段是另一个类对象User的ArrayList。但是,每当我尝试返回一个值时,它只返回其他字段。

我附上了模型类和服务的代码。你能帮助我找出解决这个问题的最佳方法。提前谢谢。

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>

1 个答案:

答案 0 :(得分:0)

我的猜测是ChatBox实例中的用户列表为空或空。

检索用户列表的方式很奇怪。你有AllServices和UserSevices类,它们都返回一个用户列表,你的User类构造函数调用其中一个方法来获取用户列表(即使该列表没有在User类中使用。这可能会导致递归如果AllServices.getUsers方法尝试使用该构造函数创建User对象。

需要进行一些调试!