springboot无法以更可读的方式将ZoneDateTime转换为json响应
我正在尝试将包含springboot无法以更可读的方式将ZoneDateTime转换为json响应,json,spring-boot,java-8,jackson,Json,Spring Boot,Java 8,Jackson,我正在尝试将包含zoneDateTime的对象转换为json响应。 我得到的json响应如下 { "DateTime": { "offset": { "totalSeconds": 19800, "id": "+05:30", "rules": { "transitionRules": [], "transitions": [],
zoneDateTime
的对象转换为json响应。
我得到的json响应如下
{
"DateTime": {
"offset": {
"totalSeconds": 19800,
"id": "+05:30",
"rules": {
"transitionRules": [],
"transitions": [],
"fixedOffset": true
}
},
"zone": {
"id": "Asia/Calcutta",
"rules": {
"transitionRules": [],
"transitions": [
{
"offsetBefore": {
"totalSeconds": 21208,
"id": "+05:53:28",
"rules": {
"transitionRules": [],
"transitions": [],
"fixedOffset": true
}
}
如何将此响应转换为特定格式,例如“dd::MM::yyyy”
我尝试在对象映射器中使用日期格式化程序,但没有按预期工作
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm");
objectMapper.setDateFormat(dateFormat);
return objectMapper;
还尝试使用@JsonFormat(pattern=“dd::MM::yyyy”)注释。我没有给出结果
public class Response {
@JsonProperty(
value = "DateTime",
required = false
)
@XmlElement(
name = "DateTime",
required = false
)
@JsonFormat(pattern = "dd::MM::yyyy")
private ZonedDateTime dateTime;
确保将
jackson-datatype-jsr310
作为依赖项。此模块包含所有java.time
类的序列化器和反序列化器,这些类还应支持指定自定义模式
spring boot配置的默认ObjectMapper
应该自动从类路径获取该模块
如果您自己配置ObjectMapper
,则需要注册模块
objectMapper.registerModule(new JavaTimeModule());
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
您可以尝试不带任何配置的
@JsonFormat(pattern=“yyyy-MM-dd'T:HH:MM:ss.SSSZ”)
或@JsonFormat(pattern=“yyy-MM-dd'T'HH:MM:ss.SSSX”)
格式吗?Spring的默认ObjectMapper
应该(并且将)处理这个问题ZonedDateTime
将接受ISO日期时间格式,可以详细查看这些格式
注意:如果您不关心区域(例如:
Asia/Calcutta
),您可以使用OffsetDateTime
,它将为您提供区域偏移量,如+05:30
,以及相同的(如上所述)可以使用模式。是否有原因不能返回字符串而不是ZonedDateTime
?请发布代表日期的整个JSON
?以上一项是不完整的。