JAXB - 从URL反序列化

5
我想显示此网站的游戏标题和ID: http://thegamesdb.net/api/GetGame.php?id=2 当我从此URL中解组时: http://www.w3schools.com/xml/note.xml 一切都很顺利,但这里只有一个对象,而不是列表。所以我现在遇到了问题。我阅读了一些来自Google的教程和示例,并编写了以下代码:
Data.java:
@XmlRootElement( name = "Data" )
@XmlAccessorType (XmlAccessType.FIELD)
public class Data {
    @XmlElement(name = "Game")
    List<Game> games;

    public List<Game> getGames() {
        return games;
    }

    public void setGames(List<Game> games) {
        this.games = games;
    }
}

Game.java文件:

@XmlRootElement(name = "Game")
@XmlAccessorType (XmlAccessType.FIELD)
public class Game {
    private int id;
    private String gameTitle;

    public int getId(){
        return id;
    }

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

    public String getGameTitle(){
        return gameTitle;
    }

    public void setGameTitle(String gameTitle){
        this.gameTitle = gameTitle;
    }
}

控制器:

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView home(Locale locale) throws MalformedURLException {
    ModelAndView model = new ModelAndView("index");
    JAXBExample test = new JAXBExample();
    Game customer = test.readXML();
    model.addObject("customer", customer);
    return model;
}

JAXBExample.java:

public class JAXBExample {
    public Game readXML() throws MalformedURLException {
        Data customer = null;
        Game game = null;
        try {
            JAXBContext jaxbContext  = JAXBContext.newInstance(Data.class);
            URL url = new URL("http://thegamesdb.net/api/GetGame.php?id=2");
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            customer = (Data) jaxbUnmarshaller.unmarshal(url);
            List<Game> games = customer.getGames();
            game = games.get(0);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return game;
    }
}

还有index.jsp页面:

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>

<tiles:insertDefinition name="defaultTemplate">
    <tiles:putAttribute name="body">
        Name: ${customer.gameTitle}<br />
        Id: ${customer.id}<br />
    </tiles:putAttribute>
</tiles:insertDefinition>

但是我的代码没有起作用。也许有人知道我做错了什么?因为我只得到了这样的结果:
Name:
Id: 

仅此而已。


你跟随了哪些教程?我需要用Spring Boot做同样的事情。 - JayC
1个回答

0
你的注释唯一的问题就是:
public class Game {
  private int id;
  @XmlElement(name = "GameTitle") //You need to add this since first letter is uppercase, otherwise the GameTitle will not unmarshall.
  private String gameTitle; 
  ... your code ...
}

那么为什么其他部分不起作用呢?

服务器返回HTTP响应代码:403,URL为:http://thegamesdb.net/api/GetGame.php?id=2

403 = 禁止访问

解决方案(让服务器认为你是浏览器)

URL url = new URL("http://thegamesdb.net/api/GetGame.php?id=2");
HttpURLConnection http = (HttpURLConnection) url.openConnection(); 
http.addRequestProperty("User-Agent", "Mozilla/4.76"); 
InputStream is = http.getInputStream();
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
customer = (Data) jaxbUnmarshaller.unmarshal(is);
List<Game> games = customer.getGames();
game = games.get(0);

注意:在此示例之外,Try catch final、关闭流以及检查NullPointer等操作由用户自行处理。

为什么你使用了“game = games.get(0);”?我按照你的步骤操作,但是我的第二个子元素为空/为null。 - Mike.Chun
1
game = games.get(0); 是来自 OP 代码,用于检索第一个 <Game> 节点。 - Petter Friberg
当我尝试从“JAXBExample.java”打印列表时,我得到[first_value, blank]。如果我将完全相同的代码复制到我的主类中,则会得到[first_value,Second_value]。 - Mike.Chun
@Mike.Chun 我其实不太明白你在搞什么,OP的代码是用来获取第一个Game节点的..也就是列表中的第一个元素。我猜列表的长度取决于游戏的ID... - Petter Friberg
@PetterFriberg,您是否知道在Spring Boot中如何实现这个功能? - JayC
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接