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

Posted

技术标签:

【中文标题】无法在 REST WS 中反序列化 LocalDate 值【英文标题】:Failing to Deserialize LocalDate Value in REST WS 【发布时间】:2019-07-07 00:22:27 【问题描述】:

我使用一个非常简单的REST WSSpringBootPostMan 来发送、接收和显示数据。问题是,反序列化 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
@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();
        
    

【问题讨论】:

【参考方案1】:

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

【讨论】:

这就是我所需要的。谢谢。

以上是关于无法在 REST WS 中反序列化 LocalDate 值的主要内容,如果未能解决你的问题,请参考以下文章

无法在 Spring Boot 中反序列化嵌套对象“Role”

C# Restful WCF 服务。无法在帖子正文中反序列化 XML

无法在 C# 中反序列化 JSON 结果。输入字符串格式不正确错误

Spring Boot REST 服务:JSON 反序列化不起作用

在 C# 中反序列化复杂对象

无法从 VALUE_STRING 令牌中反序列化 java.lang.String[] 的实例