4

我正在尝试开发一个简单的 RESTful Web 服务客户端。使用 java 和 javax jersey+dependencies。我的问题是当试图从对象进来的 XML 转换时,它返回一个空对象。

该测试是一个简单的测试。我希望获得具有特定 id 的语言,从我的测试中我可以看到我获得的 XML 与浏览器中的相同。

类的代码如下:

package data;

import javax.xml.bind.annotation.*;

@XmlRootElement(name = "prestashop")
@XmlType
public class Language {

    private int id;
    private String name;
    private String iso_code;
    private String language_code;
    private boolean active;
    private boolean is_rtl;
    private String date_format_lite;
    private String date_format_full;

    public Language() {
    }

    public Language(String name, String iso_code, String date_format_lite, 
        String date_format_full) {

        this.name = name;
        this.iso_code = iso_code;
        this.date_format_lite = date_format_lite;
        this.date_format_full = date_format_full;
    }

    public Language(int id, String name, String iso_code, String language_code, 
        boolean active, boolean is_rtl, String date_format_lite, 
        String date_format_full) {

        this.id = id;
        this.name = name;
        this.iso_code = iso_code;
        this.language_code = language_code;
        this.active = active;
        this.is_rtl = is_rtl;
        this.date_format_lite = date_format_lite;
        this.date_format_full = date_format_full;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIso_code() {
        return iso_code;
    }

    public void setIso_code(String iso_code) {
        this.iso_code = iso_code;
    }

    public String getLanguage_code() {
        return language_code;
    }

    public void setLanguage_code(String language_code) {
        this.language_code = language_code;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public boolean isIs_rtl() {
        return is_rtl;
    }

    public void setIs_rtl(boolean is_rtl) {
        this.is_rtl = is_rtl;
    }

    public String getDate_format_lite() {
        return date_format_lite;
    }

    public void setDate_format_lite(String date_format_lite) {
        this.date_format_lite = date_format_lite;
    }

    public String getDate_format_full() {
        return this.date_format_full;
    }

    public void setDate_format_full(String date_format_full) {
        this.date_format_full = date_format_full;
    }

    @Override
    public String toString(){
         return "Language ["
                + "id=" + id + ", "
                + "name=" + name + ", "
                + "iso_code=" + iso_code + ", "
                + "language_code=" + language_code + ", "
                + "active=" + active + ", "
                + "is_rtl=" + is_rtl + ", "
                + "date_format_lite=" + date_format_lite + ", "
                + "date_format_full=" +  date_format_full+ ","
                + " ]";
   }
}

主要的

package webservicetest2;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import data.Language;
import java.net.URI;
import javax.ws.rs.core.MediaType;

import javax.ws.rs.core.UriBuilder;
import javax.xml.bind.JAXBException;

public class WebServiceTest2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws JAXBException {
        try {
            ClientConfig config = new DefaultClientConfig();
            Client client = Client.create(config);
            client.addFilter(new HTTPBasicAuthFilter("KEY", ""));
            WebResource service = client.resource(getBaseURI());

            System.out.println(service.path("languages")
                    .path("7")
                    .accept(MediaType.TEXT_XML)
                    .get(String.class));

            Language language;
            language = (Language) service.path("languages")
                    .path("7")
                    .accept(MediaType.TEXT_XML)
                    .get(Language.class);
            System.out.println(language.toString());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    private static URI getBaseURI() {
        return UriBuilder.fromUri("http://localhost:8080/api").build();
    }
}

输出

run:
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<language>
    <id><![CDATA[7]]></id>
    <name><![CDATA[Danish]]></name>
    <iso_code><![CDATA[da]]></iso_code>
    <language_code><![CDATA[da]]></language_code>
    <active><![CDATA[1]]></active>
    <is_rtl><![CDATA[0]]></is_rtl>
    <date_format_lite><![CDATA[Y-m-d]]></date_format_lite>
    <date_format_full><![CDATA[Y-m-d H:i:s]]></date_format_full>
</language>
</prestashop>

Language [id=0, name=null, iso_code=null, language_code=null, active=false, is_rtl=false, date_format_lite=null, date_format_full=null, ]
BUILD SUCCESSFUL (total time: 0 seconds)

如您所见,返回的 XML 即时消息有一个对象,但该对象的打印是一个空对象。所以出了点问题。

抱歉,如果有人提出了类似的问题,但找不到任何问题。

4

0 回答 0