我正在使用Postman模拟我在Java到MYSQL数据库中创建的方法的API调用。
当我以JSON格式发送GET请求时,结果运行良好。但是,对于XML格式的相同方法,日期不会返回。逻辑和路径查询似乎很好,因为我能够以JSON格式获取完整列表。但是我缺少XML中的日期。
感谢任何人都可以指出我所缺少的东西。让我知道您是否需要其他信息。
这是不同收益的快照。
型号 PickUpLocationModel
@XmlRootElement(name = "pickuplocation")
public class PickUpLocationModel {
@XmlElement(name = "id")
public int Id;
@XmlElement(name = "userId")
public int UserId;
@XmlElement(name = "lattitude")
public double Lattitude;
@XmlElement(name = "longitude")
public double Longitude;
@XmlElement(name = "locationame")
public String LocationName;
@XmlElement(name = "dateadded")
public Date DateAdded;
@XmlElement(name = "category")
public String Category;
}
PagedPickUpLocationsModel
@XmlRootElement(name = "pickuplocations")
public class PagedPickUpLocationsModel {
@XmlElement(name = "data")
public ArrayList<PickUpLocationModel> Data;
@XmlElement(name = "next")
public String Next;
@XmlElement(name = "prev")
public String Prev;
public PagedPickUpLocationsModel() {
Data = new ArrayList<PickUpLocationModel>();
Next = "";
Prev = "";
}
}
路径
@GET
@Path("{user_id}/pickuplocations")
@Produces({"application/json;qs=0.9","application/xml;qs=0.1"})
public Response GetAllByUser(@PathParam("user_id") int userId,
@QueryParam("page") int pageNumber,
@QueryParam("pagesize") int pageSize) {
PagedPickUpLocationsModel locations = pickUpLocationService.GetPickUpLocationsByUser(userId, pageNumber, pageSize);
return Response.ok().entity(new GenericEntity<PagedPickUpLocationsModel>(locations) {}).build();
}
逻辑
public ArrayList<PickUpLocationModel> GetPickUpLocationsByUser(int user_id, int page, int pageSize) {
ArrayList<PickUpLocationModel> locations = new ArrayList<PickUpLocationModel>();
int startFrom = (page - 1) * pageSize;
Connection conn = DataConnection.GetConnection();
try {
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM user_pickup_locations WHERE user_id = ? LIMIT ?,?");
stmt.setInt(1, user_id);
stmt.setInt(2, startFrom);
stmt.setInt(3, pageSize);
ResultSet result = stmt.executeQuery();
while(result.next()) {
PickUpLocationModel location = new PickUpLocationModel();
location.Id = result.getInt("id");
location.UserId = result.getInt("user_id");
location.Lattitude = result.getDouble("lattitude");
location.Longitude = result.getDouble("longitude");
location.LocationName = result.getString("location_name");
location.DateAdded = result.getDate("date_added");
location.Category = result.getString("category");
locations.add(location);
}
} catch (Exception e) {
e.printStackTrace();
}
return locations;
}