gpt4 book ai didi

java - 自定义即时解串器无法正常工作。不断收到 HttpMessageNotReadableException

转载 作者:行者123 更新时间:2023-12-01 16:38:38 24 4
gpt4 key购买 nike

我正在尝试使用 postman 并将这些值放入数据库中,但我不断收到异常。

我试图从 postman 反序列化什么:

{
"end_date": "2443-11-34 12:43:23",
"start_date": "2443-11-34 12:43:23"
}

我得到的异常:

2020-05-20 10:55:04.572  WARN 4452 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver :
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot
deserialize value of type `java.time.Instant` from String "2443-11-34 12:43:23": Failed to deserialize
java.time.Instant: (java.time.format.DateTimeParseException) Text '2443-11-34 12:43:23' could not be
parsed at index 10; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot
deserialize value of type `java.time.Instant` from String "2443-11-34 12:43:23": Failed to deserialize
java.time.Instant: (java.time.format.DateTimeParseException) Text '2443-11-34 12:43:23' could not be parsed at index 10
at [Source: (PushbackInputStream); line: 3, column: 17] (through reference chain:
com.project.rushhour.model.post.AppointmentPostDTO["end_date"])]

预约实体:

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Appointment extends BaseEntity {

@NotNull
@DateTimeFormat(style = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
@JsonDeserialize(using = JacksonInstantDeserializer.class)
private Instant startDate;

@NotNull
@JsonDeserialize(using = JacksonInstantDeserializer.class)
@DateTimeFormat(style = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private Instant endDate;

我的类(class)预约:

@Data
@NoArgsConstructor
@AllArgsConstructor
public abstract class AppointmentDTO extends BaseDTO {

@JsonProperty("start_date")
private Instant startDate;

@JsonProperty("end_date")
private Instant endDate;

我使用的 AppointmentGetDTO 类

public class AppointmentGetDTO extends AppointmentDTO {
}

我还拥有所有 Jackson 依赖项

我使用的自定义反序列化器:

public class JacksonInstantDeserializer extends StdDeserializer<Instant> {
public JacksonInstantDeserializer() { this(null); }
public JacksonInstantDeserializer(Class<?> clazz) { super(clazz); }

@Override
public Instant deserialize(JsonParser parser, DeserializationContext ctx) throws IOException {
return Instant.parse(parser.getText());
}
}

最佳答案

您应该为您的类 AppointmentGetDTO 而不是 Instant 类创建一个 CustomDeserializer

我们需要使用Deserializer注册AppointmentDTO类。下面我提供了相关的代码更改。用户调试器并在解串器中创建断点来测试转换逻辑。

如需进一步阅读,请结账:jackson-deserialization还有这个 Stackoverflow answer

阅读本文以了解替代方法:JsonComponent

预约DTO:

@Data
@NoArgsConstructor
@AllArgsConstructor
public abstract class AppointmentDTO {

@JsonProperty("start_date")
private Instant startDate;

@JsonProperty("end_date")
private Instant endDate;
}

预约GetDTO:

@JsonDeserialize(using= JacksonInstantDeserializer.class)
public class AppointmentGetDTO extends AppointmentDTO {
public AppointmentGetDTO(Instant s, Instant e) {
super(s,e);
}
}

用于 AppointmentGetDTO 的自定义反序列化器

public class JacksonInstantDeserializer extends StdDeserializer<AppointmentGetDTO> {
public JacksonInstantDeserializer() { this(AppointmentDTO.class); }
public JacksonInstantDeserializer(Class<?> clazz) { super(clazz); }

@Override
public AppointmentGetDTO deserialize(JsonParser parser, DeserializationContext ctx) throws IOException {
JsonNode node = parser.getCodec().readTree(parser);
Instant s=null;
Instant e=null;
if(node.get("start_date") != null) {
s=Instant.parse(node.get("start_date").asText());
}
if(node.get("end_date")!=null) {
e=Instant.parse(node.get("end_date").asText());
}
return new AppointmentGetDTO(s,e);
}
}

然后你可以创建com.fasterxml.jackson.databind.Module的bean,如下所示:

    @Bean
public Module dynamoDemoEntityDeserializer() {
SimpleModule module = new SimpleModule();
module.addDeserializer(AppointmentGetDTO.class, new JacksonInstantDeserializer());
return module;
}

请求的 Controller 映射:

@PostMapping
public AppointmentDTO convert(@RequestBody AppointmentGetDTO appointmentDTO) {
System.out.println(appointmentDTO.getStartDate());
System.out.println(appointmentDTO.getEndDate());
return appointmentDTO;
}

请求 json:

{
"end_date": "2443-11-12T12:43:23Z",
"start_date": "2443-11-12T12:43:23Z"
}

关于java - 自定义即时解串器无法正常工作。不断收到 HttpMessageNotReadableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61908218/

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