0

i am using Postman to simulate API calls for methods i have created in java to MYSQL database.

When i send GET request in JSON format, the results works fine. However, for the same method in XML format, the date does not return. The logic and path query seems fine because i am able to get the full list in the JSON format. But i am missing the Date in XML.

Appreciate if anyone can point out what i am missing. Let me know if you need any further information.

This is a snapshot of the different return.

enter image description here

enter image description here

Models 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 = "";
    }
}

Path

@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();
    }

Logic

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;
    }
DWing
  • 41
  • 7
  • I'm not sure, but probably you need to specify XmlAdapter for you DateAdded field. https://stackoverflow.com/a/9268488/5940335 – Mikhail Baksheev Aug 09 '18 at 16:51
  • @MikhailBaksheev Yes, you are right! Not sure why, but JAXB doesn't support XML for java.sql.Date. Thanks! – DWing Aug 10 '18 at 03:53

0 Answers0