我在哪个类中使用@XMLElement注释?
URL:http://localhost:8080/GigstartServices/GET/Users/2/Orders
我希望它是
<Orders>
<Order>
</Order>
</Orders>
而不是
<Orderss>
<Orders>
</Orders>
</Orderss>
我的项目中有以下Orders课程。
OrderResource.java:
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import com.dto.Orders;
public class OrderResource {
int total;
public ArrayList<Orders> GetOrders(Connection connection, int id) throws Exception
{
ArrayList<Orders> feedData = new ArrayList<Orders>();
ArrayList<Integer> itemsId;
try
{
PreparedStatement ps = connection.prepareStatement("SELECT o.id , o.userId FROM dbo.Orders o join dbo.Items I on o.itemId = I.id join dbo.Users u on o.UserId = u.Id where o.userId = ? group by o.id, o.userId");
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
while(rs.next())
{
Orders o = new Orders();
o.setId(rs.getInt("id"));
ps = connection.prepareStatement("SELECT o.ItemId, price FROM dbo.Orders o join dbo.Items I on o.itemId = I.id WHERE o.UserId= ? and o.Id = ?");
ps.setInt(1, rs.getInt("userId"));
ps.setInt(2, rs.getInt("id"));
ResultSet rs2 = ps.executeQuery();
itemsId = new ArrayList<Integer>();
total = 0;
while(rs2.next()){
itemsId.add(rs2.getInt("ItemId"));
total = total + rs2.getInt("price");
}
o.setItem(itemsId);
o.setUserId(rs.getInt("userId"));
o.setTotalAmount(total);
feedData.add(o);
}
return feedData;
}
catch(Exception e)
{
throw e;
}
}
}
package com.webService;
import java.sql.Connection;
import java.util.ArrayList;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import com.dao.Database;
import com.dao.OrderResource;
import com.dao.Resources;
import com.dto.Items;
import com.dto.Orders;
@Path("/")
public class OrderService {
OrderService(){
}
ArrayList<Orders> u = null;
Database database= new Database();
@GET
@Produces(MediaType.APPLICATION_XML)
public ArrayList<Orders> getOrders(@PathParam("userid") int userid) throws Exception
{
Connection connection = database.GetConnection();
OrderResource ur= new OrderResource();
u=ur.GetOrders(connection, userid);
return u;
}
}
3
package com.dto;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Orders {
private int id;
private ArrayList<Integer> items;
private int userId;
private int totalAmount;
public Orders(){
}
private ArrayList<Integer> Items;
public Orders(int id,ArrayList<Integer> items, int userId , int totalAmount){
Items = new ArrayList<Integer>();
this.id = id;
this.items = items;
this.userId = userId;
this.totalAmount = totalAmount;
}
/**
* @return the Id
*/
public int getId() {
return id;
}
/**
* set Id
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the ItemId
*/
/*@XmlElementWrapper(name="Items")
@XmlElement(name="Item")
public ArrayList<Integer> getItemId() {
return Items;
}
/**
* @set name
*/
/*public void setItem(ArrayList<Integer> items) {
this.items = items;
}
*/
@XmlElementWrapper(name="Items")
@XmlElement(name="Item")
public ArrayList<Integer> getItemId() {
return items;
}
/**
* @set name
*/
public void setItem(ArrayList<Integer> items) {
this.items = items;
}
/**
* @return price
*/
public int getUserId() {
return userId;
}
/**
* set price
*/
public void setUserId(int userId) {
this.userId = userId;
}
public int getTotalAmount(){
return totalAmount;
}
public void setTotalAmount(int totalAmount){
this.totalAmount = totalAmount;
}
}
答案 0 :(得分:1)
在Orders
课程中,将name
添加到@XmlRootElement
。默认值是类的名称。因此,每个订单都是<Orders>
,当它被包装时(因为它在列表中),包装元素只会附加一个s
,它将为您提供<Orderss>
。所以只需将name
设置为Order
@XmlRootElement(name = "Order")
public class Orders
或者只是将类的名称更改为Order
,这对我来说实际上在语义上更有意义,因为类的实例只是一个订单,而不是复数(多个订单)。