I'm tryting to display title and id of game from this site: http://thegamesdb.net/api/GetGame.php?id=2
When I was unmarshalling from this url: http://www.w3schools.com/xml/note.xml it was all ok, but here was just one object, not a list. So I'm having problem now. I was reading some tutorials and examples from Google and I made this code:
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 file:
@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;
}
}
Controller:
@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;
}
}
And 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>
But my code isn't working. Somebody have maybe idea what I'm doing wrong? Because as result I just get:
Name:
Id:
And nothing more.