如何在 openfire rest API 中从 restclient.entity.UserEntities 中提取数据?

how to extract data from restclient.entity.UserEntities in openfire rest API?

我正在使用这个:Openfire Rest API

现在我正在使用 java 文件获取用户和组。 作为回应,我期待 XML 数据,但它向我显示了奇怪的数据。

我是 Java 的新手,所以我不知道如何从中提取数据。

我的密码是:

package bizrtc;

import api.restclient.RestClient;
import api.restclient.RestApiClient;
import api.restclient.entity.AuthenticationToken;
import api.restclient.entity.UserEntities;
import api.restclient.entity.GroupEntities;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 *
 * @author Rajan
 */
public class Bizrtc 
{
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
       // TODO code application logic here
       AuthenticationToken authenticationToken = new AuthenticationToken("cn1ed9s8yEf5woQV");
       // Set Openfire settings (9090 is the port of Openfire Admin Console)
       RestApiClient restApiClient = new RestApiClient("192.168.50.50", 9090, authenticationToken);
      // restApiClient.getUsers();
       UserEntities users = restApiClient.getUsers();

       System.out.println("The Groups are as below: "+restApiClient.getGroups());

       System.out.println("Now fetching data from openfire Server");
       System.out.println("The data is *******************************" + users.toString());

    }



}

当我 运行 程序时,我得到:

Dec 23, 2016 3:58:43 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Sending client request on thread main
1 > GET http://192.168.50.50:9090/plugins/restapi/v1/groups
1 > Authorization: cn1ed9s8yEf5woQV
1 > Content-Type: application/xml

Dec 23, 2016 3:58:44 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Client response received on thread main
1 < 200
1 < Access-Control-Allow-Credentials: true
1 < Access-Control-Allow-Headers: origin, content-type, accept, authorization
1 < Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS, HEAD
1 < Access-Control-Allow-Origin: *
1 < Content-Length: 3664
1 < Content-Type: application/xml
1 < Date: Fri, 23 Dec 2016 09:53:38 GMT
1 < Expires: Thu, 01 Jan 1970 00:00:00 GMT
1 < Set-Cookie: JSESSIONID=1bt213yrejbmfkpyfs53snplm;Path=/;HttpOnly
1 < X-Frame-Options: deny

The Groups are as below: api.restclient.entity.GroupEntities@1165b38
Now fetching data from openfire Server
The data is *******************************api.restclient.entity.UserEntities@4c12331b

如何以 XML 或更好的 ARRAY 格式打印此用户??

如何从 response:api.restclient.entity.GroupEntities@1165b38

获取用户

我必须将其转换为字符串或类似的东西吗?

看看UserEntitiesJava代码:

@XmlRootElement(
    name = "users"
)
public class UserEntities {
    List<UserEntity> users;

    public UserEntities() {
    }

    public UserEntities(List<UserEntity> users) {
        this.users = users;
    }

    @XmlElement(
        name = "user"
    )
    public List<UserEntity> getUsers() {
        return this.users;
    }

    public void setUsers(List<UserEntity> users) {
        this.users = users;
    }
}

它是一个 POJO class,带有用户列表并映射有 JAXB 注释。这意味着您可以轻松地将对象转换为 XML、JSON 或 lib 启用的任何内容。

XML方式:

JAXBContext jaxbContext = JAXBContext.newInstance(UserEntities.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(users, sw);
String xmlString = sw.toString();

System.out.println(xmlString);

如果你想要一个 UserEntity 的数组,你已经有了它的 List:

final UserEntity[] arrayUsers = (UserEntity[]) users.getUsers().toArray();

Return 示例:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<users>
    <user>
        <username>d</username>
        <name>e</name>
        <email>a@a.com</email>
        <password>pass</password>
    </user>
</users>