Muthukumaran K
Muthukumaran K

Reputation: 63

Data mismatch between Json response and database

I'm retrieving data from mysql using spring boot, hibernate and producing the response as REST service. But while checking in Postman the data mismatches. Here is my code.

Controller Class:

@RestController
public class NotificationController<T> extends RestUtils<T> {
    @Autowired
    NotificationService<T> service;

    @RequestMapping(value = "/notification/getNotifications", method = RequestMethod.POST, headers = "Accept=application/json")
    public @ResponseBody Object getNotifications(@RequestBody NotificationBean notificationBean) {
        try {
            return getSuccessResponse(service.getNotifications(notificationBean), "Total Count: ",
                    service.getNotificationsCount(notificationBean));
        } catch (StudawnException e) {
            return getErrorResponse(e.getMessage());
        }
    }
}

Service Implementation:

@Service
public class NotificationServiceImpl<T> implements NotificationService<T> {
    @Autowired
    NotificationRepository notificationRepository;

    @SuppressWarnings("deprecation")
    @Transactional
    @Override
    public Object getNotifications(NotificationBean notificationBean) throws StudawnException {
        Sort sort = new Sort(new Sort.Order(Direction.DESC, "dateCreated"));
        Pageable pageable = new PageRequest(notificationBean.getPage(), notificationBean.getSize(), sort);
        return notificationRepository.findNotificationsByStudentId(notificationBean.getStudentId(), pageable);
    }

}

Repository:

@Repository
public interface NotificationRepository<T> extends JpaRepository<Notification, Integer> {
    @Query(NotificationQuery.findNotificationsByStudentId)
    List<Notification> findNotificationsByStudentId(@Param("studentId") Integer studentId, Pageable pageable);

    @Query(NotificationQuery.findNotificationsCountByStudentId)
    Integer findNotificationsCountByStudentId(@Param("studentId")Integer studentId);
}

Query class:

public class NotificationQuery {
    public static final String findNotificationsByStudentId = "select n from #{#entityName} n where n.student.studentId = :studentId and n.isActive=true";
    public static final String findNotificationsCountByStudentId = "select count(n) from #{#entityName} n where n.student.studentId=:studentId and n.isActive=true";
}

Model class

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "notification")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = { "dateCreated" }, allowGetters = true)
public class Notification {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "notification_id")
    private int notificationId;

    @ManyToOne
    @JoinColumn(name = "student_id")
    private Student student;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_date", nullable = false, updatable = false)
    @CreatedDate
    private Date dateCreated;

    @Column(name = "is_active")
    private boolean isActive;

    @Column(name = "has_read")
    private boolean hasRead;

    @Column(name = "message", length = 150)
    private String message;

    public int getNotificationId() {
        return notificationId;
    }

    public void setNotificationId(int notificationId) {
        this.notificationId = notificationId;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }

    public boolean isHasRead() {
        return hasRead;
    }

    public void setHasRead(boolean hasRead) {
        this.hasRead = hasRead;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

Json Request

{
    "studentId":101,
    "page":0,
    "size":10
}

Json Response The date in 'dateCreated' field mismatch with database. The actual date in DB is '2018-03-06' but in response it is '2018-03-05'

 {
        "response": [
            {
                "notificationId": 4,
                "student": {
                    "studentId": 101,
                    "studawnId": "1234",
                    "firstName": "arun",
                    "lastName": "kumar",
                    "emailId": "[email protected]",
                    "mobileNumber": "987654",
                    "dateOfBirth": "1990-03-02T18:30:00.000+0000",
                    "gender": "male",
                    "profilePicture": "Pictures",
                    "hasTermsAccepted": true,
                    "dateCreated": "2018-03-02T18:30:00.000+0000",
                    "dateModified": "2018-03-02T18:30:00.000+0000",
                    "aadhaarNumber": "565497",
                    "addressOne": "adyar",
                    "addressTwo": "chennai",
                    "pincode": "600096",
                    "tickets": [],
                    "active": true,
                    "firstTimeLogin": true,
                    "school": true,
                    "college": true
                },
                "dateCreated": "2018-03-05T18:30:00.000+0000",
                "hasRead": true,
                "message": "User 1 has sent a msg to you",
                "active": true
            }
    ]
    }

Thanks in advance.

Upvotes: 0

Views: 475

Answers (1)

korrauk
korrauk

Reputation: 26

I think you should check timezones. It looks like you have saved the date in your local timezone, but when you retrieve it from database and send to client you use GMT timezone.

Upvotes: 1

Related Questions