gpt4 book ai didi

java - 无法在 REST WS 中反序列化 LocalDate 值

转载 作者:行者123 更新时间:2023-11-30 05:46:55 27 4
gpt4 key购买 nike

我使用非常简单的 REST WS 以及 SpringBootPostMan 来发送、接收和显示数据。问题是,反序列化 Date 值似乎存在问题,并且由于某种原因,它只接受这种格式 yyyy-mm-dd(用破折号分隔)。

因此,例如,如果我从 Postman 发送以下数据,我会毫无问题地收到响应:

{  
"server":{
"address":"url",
"port":2555525
},
"date":"2019-02-13"
}

但是,如果我执行以下操作(不带破折号的起息日期):

{  
"server":{
"address":"url",
"port":2555525
},
"date":"20190213"
}

我收到以下错误:

{
"timestamp": 1550046097893,
"status": 400,
"error": "Bad Request",
"message": "JSON parse error: Cannot deserialize value of type `java.time.LocalDate` from String \"20190213\": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '20190213' could not be parsed at index 0; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String \"20190213\": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '20190213' could not be parsed at index 0\n at [Source: (PushbackInputStream); line: 6, column: 9] (through reference chain: com.restws.webservices.model.Admin[\"date\"])",
"path": "/v3/admin/device"
}

我想要实现的是能够反序列化日期,无论 PostMan 中输入的格式如何。有办法做到这一点吗?

这是我的 Controller :

@Controller
@RequestMapping("/v3/admin/")
public class AdminController {

Logger logger = LoggerFactory.getLogger(AdminController.class);

@Autowired
private ObjectMapper objectMapper;

@RequestMapping(method = RequestMethod.POST, value = "/device", consumes ="application/json")
public ResponseEntity putDevice(@RequestBody Admin admin) throws IOException {

logger.info(admin.toString());
String adminString = admin.toString();
return new ResponseEntity<>(adminString, HttpStatus.OK);

}
}

还有 POJO:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Admin {

@JsonProperty("server")
private Server server;

@JsonProperty("date")
private LocalDate date;

public Admin() {
}


public Server getServer() {
return server;
}

public void setServer(Server server) {
this.server = server;
}

public LocalDate getDate() {
return date;
}

public void setDate(LocalDate date) {
this.date = date;
}

@Override
public String toString() {
return new ToStringBuilder(this).append("server", server).append("date", date).toString();
}
}

//

 public class Server {

@JsonProperty("address")
private String address;

@JsonProperty("port")
private int port;

public Server() {
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

Server server = (Server) o;

return new EqualsBuilder().append(port, server.port).append(address, server.address).isEquals();
}

@Override
public int hashCode() {
return new HashCodeBuilder(17, 37).append(address).append(port).toHashCode();
}

@Override
public String toString() {
return new ToStringBuilder(this).append("address", address).append("port", port).toString();
}
}

最佳答案

您可以尝试编写自定义 JsonDeserializer 以您想要接受的格式处理日期反序列化: JSON serialization strategy for dates

关于java - 无法在 REST WS 中反序列化 LocalDate 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54665596/

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