gpt4 book ai didi

mysql - Json响应与数据库数据不匹配

转载 作者:行者123 更新时间:2023-11-29 10:14:10 24 4
gpt4 key购买 nike

我正在使用 spring boot、hibernate 从 mysql 检索数据,并将响应作为 REST 服务生成。但在检查 postman 时,数据不匹配。这是我的代码。

Controller 类:

@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
}
]
}

提前致谢。

最佳答案

我认为你应该检查时区。看起来您已经将日期保存在本地时区中,但是当您从数据库检索它并将其发送到客户端时,您使用的是 GMT 时区。

关于mysql - Json响应与数据库数据不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50389305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com