JAXB - 从url解组 [英] JAXB - unmarshalling from url
问题描述
我正在尝试从此网站显示游戏的标题和ID:
http://thegamesdb.net/api/GetGame.php?id=2
I'm tryting to display title and id of game from this site: http://thegamesdb.net/api/GetGame.php?id=2
当我从这个网址解组时: http://www.w3schools.com/xml/note.xml 一切都好,但这里只是一个对象,而不是列表。所以我现在遇到问题。我正在阅读谷歌的一些教程和示例,我制作了这段代码:
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:
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文件:
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;
}
}
控制器:
@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:
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:
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:
仅此而已。
推荐答案
唯一的事情你的注释有问题是
The only thing that is wrong with your annotation is
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
解决方案(让服务器相信您是浏览器)
Solution (make the server believe you are a browser)
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);
注意:尝试捕获最终
,关闭流并且检查 NullPointer
超出了这个例子而且取决于用户。
Note: Try catch final
, close streams and checking for NullPointer
is beyond this example and up to user.
这篇关于JAXB - 从url解组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!