Я извлекаю данные из mysql, используя весеннюю загрузку, режим гибернации, и выдаю ответ в качестве службы REST.Но при проверке в Postman данные не совпадают.Вот мой код.
Класс контроллера:
@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
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
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);
}
Класс запроса:
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";
}
Класс модели
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
{
"studentId":101,
"page":0,
"size":10
}
Ответ Json Дата в поле dateCreated не соответствует базе данных.Фактическая дата в БД - «2018-03-06», но в ответ «2018-03-05»
{
"response": [
{
"notificationId": 4,
"student": {
"studentId": 101,
"studawnId": "1234",
"firstName": "arun",
"lastName": "kumar",
"emailId": "arun@xyz.com",
"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
}
]
}
Заранее спасибо.