当有人能帮助我了解为什么我收到此错误:JAXB错误:意外的元素解组
javax.xml.bind.UnmarshalException:意外的元素(URI:“”,当地“项目”) 。预期的元素是< {} item>
我刚刚接触JAX-B,但一直都停留在这一天,我真的不明白发生了什么以及任何帮助真的很感谢,非常感谢。
项目类:
@XmlRootElement
public class Item {
private String itemID;
private String itemDescription;
//need to have a constructor with no params
public Item(){
}
//Constructor: sets object vars
public Item(String itemID, String itemDescription) {
this.itemID = itemID;
this.itemDescription = itemDescription;
}
@XmlAttribute
//getters and setters
public String getID() {
return itemID;
}
public void setId(String id) {
itemID= id;
}
@XmlElement
public String getDescription() {
return itemDescription;
}
public void setDescription(String description) {
itemDescription = description;
}
解组代码:
resource = client.resource("http://localhost:8080/testProject/rest/items");
ClientResponse response= resource.get(ClientResponse.class);
String entity = response.getEntity(String.class);
System.out.println(entity);
JAXBContext context = JAXBContext.newInstance(Item.class);
Unmarshaller um = context.createUnmarshaller();
Item item = (Item) um.unmarshal(new StringReader(entity));
And this is the XML i'm trying to parse:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<items>
<item id="1">
<description>Chinos</description>
</item>
<item id="2">
<description>Trousers</description>
</item>
</items>
下面是创建XML Web服务:
@GET
@Produces(MediaType.TEXT_XML)
public List<Item> getItemsBrowser(){
java.sql.Connection connection;
java.sql.Statement statement;
List<Item> items = new ArrayList<Item>();
ResultSet resultSet = null;
try {
connection = dataSource.getConnection();
statement = connection.createStatement();
String query = "SELECT * FROM ITEMS";
resultSet = statement.executeQuery(query);
// Fetch each row from the result set
while (resultSet.next()) {
String a = resultSet.getString("itemID");
String b = resultSet.getString("itemDescription");
//Assuming you have a user object
Item item = new Item(a, b);
items.add(item);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return items;
}
告诉我们更多关于您的环境。你是否在移动代码新服务器?什么类型的web服务(首先是java代码(或)WSDL)? – kosa 2012-04-12 20:52:07
我用我的Java REST风格的web服务代码 – flexer7661 2012-04-12 21:02:02