如何将时间值从 Postman 发送到我的 REST api
How to send time value from Postman to my REST api
我有一个 class 有一个日期类型的变量,代表一个时间
@Entity
public class Product implements Serializable {
private static final long serialVersionUID = -7181205262894478929L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int productId;
@NotNull()
private String productName;
@Temporal(TemporalType.DATE)
@DateTimeFormat(style = "yyyy-MM-dd")
@NotNull()
private Date date;
@Temporal(TemporalType.TIME)
@DateTimeFormat(style = "hh:mm")
@NotNull()
private Date time;
....
}
现在我在 Postman 上尝试 CRUD 方法,当我发送时
{
"productName": "name",
"date": "2016-03-10",
"time": "10:29"
}
我明白了
400 Bad Request
带有描述:
The request sent by the client was syntactically incorrect.
当我没有时间尝试时,它就过去了。
改变这个
@Temporal(TemporalType.TIME)
@DateTimeFormat(style = "hh:mm")
@NotNull()
private Date time;
而不是
@NotNull()
private String time;
因为您尝试解析字符串值 10:29
不是您的 time
变量的有效表示
如果您使用的是Jackson,可以尝试以下解决方案:
1。使用自定义 JsonDeserializer
定义自定义 JsonDeserializer
:
public class TimeDeserializer extends JsonDeserializer<Date> {
private SimpleDateFormat format = new SimpleDateFormat("hh:mm");
@Override
public Date deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
String date = p.getText();
try {
return format.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
然后用 @JsonDeserialize
:
注释你的 time
属性
@NotNull
@Temporal(TemporalType.TIME)
@DateTimeFormat(style = "hh:mm")
@JsonDeserialize(using = TimeDeserializer.class)
private Date time;
2。使用 @JsonFormat
注释
或者,您可以尝试 @JsonFormat
注释,而不是创建自定义 JsonDeserializer
:
@NotNull
@Temporal(TemporalType.TIME)
@DateTimeFormat(style = "hh:mm")
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="hh:mm")
private Date time;
最后一件事
hh:mm
是您真正想要的格式吗?
hh
表示 1-12 格式 的小时数,而 HH
表示 0-23 格式 的小时数.如果你选择 1-12 格式,你可以考虑使用 AM/PM 标记 :hh:mm a
.
有关详细信息,请查看 SimpleDateFormat
文档。
我有一个 class 有一个日期类型的变量,代表一个时间
@Entity
public class Product implements Serializable {
private static final long serialVersionUID = -7181205262894478929L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int productId;
@NotNull()
private String productName;
@Temporal(TemporalType.DATE)
@DateTimeFormat(style = "yyyy-MM-dd")
@NotNull()
private Date date;
@Temporal(TemporalType.TIME)
@DateTimeFormat(style = "hh:mm")
@NotNull()
private Date time;
....
}
现在我在 Postman 上尝试 CRUD 方法,当我发送时
{ "productName": "name", "date": "2016-03-10", "time": "10:29" }
我明白了
400 Bad Request
带有描述:
The request sent by the client was syntactically incorrect.
当我没有时间尝试时,它就过去了。
改变这个
@Temporal(TemporalType.TIME)
@DateTimeFormat(style = "hh:mm")
@NotNull()
private Date time;
而不是
@NotNull()
private String time;
因为您尝试解析字符串值 10:29
不是您的 time
变量的有效表示
如果您使用的是Jackson,可以尝试以下解决方案:
1。使用自定义 JsonDeserializer
定义自定义 JsonDeserializer
:
public class TimeDeserializer extends JsonDeserializer<Date> {
private SimpleDateFormat format = new SimpleDateFormat("hh:mm");
@Override
public Date deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
String date = p.getText();
try {
return format.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
然后用 @JsonDeserialize
:
time
属性
@NotNull
@Temporal(TemporalType.TIME)
@DateTimeFormat(style = "hh:mm")
@JsonDeserialize(using = TimeDeserializer.class)
private Date time;
2。使用 @JsonFormat
注释
或者,您可以尝试 @JsonFormat
注释,而不是创建自定义 JsonDeserializer
:
@NotNull
@Temporal(TemporalType.TIME)
@DateTimeFormat(style = "hh:mm")
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="hh:mm")
private Date time;
最后一件事
hh:mm
是您真正想要的格式吗?
hh
表示 1-12 格式 的小时数,而 HH
表示 0-23 格式 的小时数.如果你选择 1-12 格式,你可以考虑使用 AM/PM 标记 :hh:mm a
.
有关详细信息,请查看 SimpleDateFormat
文档。