有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在使用来自webfront的jackson配对json字符串时忽略无效值并设置空值?

有没有一个好的方法来改变解析策略,甚至出现一个值不能转换为特定类型,只需设置null并继续解析

这是错误信息

15:39:29 WARN  (DefaultHandlerExceptionResolver.java:424) Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.math.BigDecimal` from String "-": not a valid representation; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.math.BigDecimal` from String "-": not a valid representation
 at [Source: (PushbackInputStream); line: 1, column: 1483] (through reference chain: com.vicson.SGHDGD.base.model.DailyOutputUpdatePojo["sheet0"]->com.vicson.SGHDGD.base.model.UpdateSheet["insertRows"]->java.util.ArrayList[1]->com.vicson.SGHDGD.base.model.DailyOutput["weekOutputDRatio"])

共 (1) 个答案

  1. # 1 楼答案

    最简单的方法是让字符串字段成功地从JSON反序列化。然后在接收字段值的setter/constructor中解析BigDecimal。像这样:

    public void setWeekOutputDRatio(String value){
      try{
        this.weekOutputDRatio = new BigDecimal(value.toCharArray())
      } catch (Exception ignored) {
        this.weekOutputDRatio = DEFAULT_VALUE;
      }
    }