简体   繁体   中英

java returning array list of one object which has field of array list another object to rest api

I am trying to return an ArrayList of chatbox class objects which has a field allowedUser that is an ArrayList of another class objects User . However whenever I try to return a value, it only returns other fields.

I have attached the code of both model class and services. Can you guys please help me to figure out the best approach to solve this. Thanks in advance.

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 output

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

My guess is that the list of users in the ChatBox instances is null or empty.

The way the user list is retrieved is odd. You have AllServices and UserSevices classes which both return a list of users and your User class constructor calls one of those methods to obtain a list of users (even though that list isn't used in the User class. This might lead to recursion if the AllServices.getUsers method tries to create User objects, using that constructor.

Some debugging is needed!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM