gpt4 book ai didi

java - 如何使用 POSTMAN JSON 发布 "Date"?

转载 作者:行者123 更新时间:2023-12-01 19:58:09 25 4
gpt4 key购买 nike

您好 StackOverflow 社区,我正在尝试测试我的 Ajax post 请求以设置包含开始日期和结束日期的预算。

我能够设置所有其他值,但不幸的是我的日期没有被推迟...有人有想法吗?

我要把我的 JS AJAX 请求测试,我的 postman 和我的 JAVA Budget Class。

再次感谢F。

package com.eBudget.eBudget;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.ManyToMany;

import org.hibernate.mapping.ForeignKey;

import javax.persistence.Id;

@Entity
public class Budget {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer idBudget;
public String nom;
public LocalDate datedeDebut;
public Float valeur;
public boolean etat;
public Date datedeFin;

@ManyToMany(mappedBy = "budgets")
public List<Users> users;

public Budget(Integer idBudget, String nom, LocalDate datedeDebut,
Float valeur, boolean etat, Date datedeFin, List<Users> users) {
this.idBudget = idBudget;
this.nom = nom;
this.datedeDebut = datedeDebut;
this.valeur = valeur;
this.etat = etat;
this.datedeFin = datedeFin;
this.users = users;
}

public Budget() {

}

public Integer getIdBudget() {
return idBudget;
}

public void setIdBudget(Integer idBudget) {
this.idBudget = idBudget;
}

public String getNom() {
return nom;
}

public void setNom(String nom) {
this.nom = nom;
}

public LocalDate getDatedeDebut() {
return datedeDebut;
}

public void setDatedeDebut(LocalDate datedeDebut) {
this.datedeDebut = datedeDebut;
}

public Float getValeur() {
return valeur;
}

public void setValeur(Float valeur) {
this.valeur = valeur;
}

public boolean isEtat() {
return etat;
}

public void setEtat(boolean etat) {
this.etat = etat;
}

public Date getDatedeFin() {
return datedeFin;
}

public void setDatedeFin(Date datedeFin) {
this.datedeFin = datedeFin;
}

public List<Users> getUsers() {
return users;
}

public void setUsers(List<Users> users) {
this.users = users;
}
}

postman 发帖请求

{ 
"id":4,
"nom":"Course3",
"dateDebut":"2019-12-12",
"valeur":200,
"etat":true,
"dateFin":"2020-01-01"
}

Ajax 请求

<script>
$("#formulaire").submit(function(e) {
e.preventDefault();

// 1ère etape test en dur //
console.log("#valideBudget");
var nom = "Course";
var dateDebut = 2019 - 12 - 12;
var valeur = 200;
var etat = true;
var dateFin = 2020 - 01 - 01;
// faire un json//
//2 creer un objet test//
var budget = {};
budget.id = 4;
budget.nom = nom;
budget.dateDebut = dateDebut;
budget.valeur = valeur;
budget.etat = etat;
budget.dateFin = dateFin;
console.log(budget);
console.log("tg");
console.log(JSON.stringify(budget));
// test//

//3 requete ajax//

$.ajax({
type: 'POST',
// objet que je lui envoie //
data: JSON.stringify(budget),
contentType: "application/json; charset= utf-8",
url: "http://localhost:8080/budget"
}).done(function(data) {
console.log("envoi effectuer");
});
});
</script>

postman 上的结果状态正常 200

{
"idBudget": 12,
"nom": "Course3",
"datedeDebut": null,
"valeur": 200.0,
"etat": true,
"datedeFin": null,
"users": null
}

最佳答案

尝试发送带引号的日期变量?

var dateDebut = "2019-12-12";

var dateFin="2020-01-01";

这对你有用吗?

-----------------更新-----------------
如果这不起作用,请尝试使用 JavaScript 日期对象;

var dateDebut = new Date(2019,11,12,0,0,0,0);

var dateFin = new Date(2020,0,1,0,0,0,0);

关于java - 如何使用 POSTMAN JSON 发布 "Date"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59021432/

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