杰克逊反序列化整数转换错误

时间:2018-10-22 15:02:02

标签: java jackson

我试图处理是否要求字段带有String值,而我希望它是Integer。例如;

{
"transactionTimeMilliseconds": "asd"
}

但它在Java代码中定义为int。

private int transactionTimeMilliseconds;
@JsonCreator
public Channel(@JsonProperty("transactionTimeMilliseconds") int transactionTimeMilliseconds) { 
        this.transactionTimeMilliseconds = transactionTimeMilliseconds;
    }

我有一个异常通知程序类。

CLASS

@ControllerAdvice
public class ExceptionConfiguration extends ResponseEntityExceptionHandler {

    @ExceptionHandler(MismatchedInputException.class) // Or whatever exception type you want to handle
    public ResponseEntity<JsonException> handleMissingFieldError(MismatchedInputException exception) { // Or whatever exception type you want to handle
        int code = 601;
        String message = exception.getMessage().split("\n")[0] + exception.getMessage().split(";")[1].replace("]", "");
        JsonException jsonException = new JsonException(code,message);
        return ResponseEntity.status(jsonException.getCode()).body(jsonException);
    }

    @ExceptionHandler(UnrecognizedPropertyException.class) // Or whatever exception type you want to handle
    public ResponseEntity<JsonException> handleUnrecognizedFieldError(UnrecognizedPropertyException exception) { // Or whatever exception type you want to handle
        int code = 602;
        String message = exception.getMessage().split(",")[0] + exception.getMessage().split(";")[1].replace("]", "");
        JsonException jsonException = new JsonException(code,message);
        return ResponseEntity.status(jsonException.getCode()).body(jsonException);
    }

    @ExceptionHandler(JsonParseException.class) // Or whatever exception type you want to handle
    public ResponseEntity<JsonException> handleJsonParseError(JsonParseException exception) {
        int code = 603;
        String message = exception.getMessage().split(":")[0] + exception.getMessage().split(";")[1].replace("]", "");
        JsonException jsonException = new JsonException(code,message);
        return ResponseEntity.status(jsonException.getCode()).body(jsonException);
    }

    @ExceptionHandler(InvalidFormatException.class) // Or whatever exception type you want to handle
    public ResponseEntity<JsonException> handleJsonInvalidFormatError(InvalidFormatException exception) {
        int code = 604;
        String message = exception.getMessage().split(":")[0] + exception.getMessage().split(";")[1].replace("]", "");
        JsonException jsonException = new JsonException(code,message);
        return ResponseEntity.status(jsonException.getCode()).body(jsonException);
    }

    @ExceptionHandler(JsonMappingException.class) // Or whatever exception type you want to handle
    public ResponseEntity<JsonException> handleNullFieldError(JsonMappingException exception) {
        int code = 605;
        String message = exception.getMessage().split(":")[0] + exception.getMessage().split(";")[1].replace("]", "");
        JsonException jsonException = new JsonException(code,message);
        return ResponseEntity.status(jsonException.getCode()).body(jsonException);
    }
}

我必须认识到该值,并且如果如上所写该字段错误,请将其默认值设置为0。

我应该编写自定义解串器来解决此问题吗?谢谢。

1 个答案:

答案 0 :(得分:0)

类似的东西对我有用:

class Val {
    private int v;

    public int getV() {
        return v;
    }

    @JsonSetter // or  @JsonProperty("v")
    public void setV(String v) {
        System.out.println("in setter");
        try {
           this.v = Integer.parseInt(v);
        } catch (Exception e) {
            this.v = 0;
        }
    }
}

测试:

@Test
public void test() throws IOException {
    String json = " { \"v\" : 1 } ";
    Val v = new ObjectMapper().readValue(json, Val.class);
    System.out.println(v.getV()); // prints 1

    json = " { \"v\" : \"asd\" } ";
    v = new ObjectMapper().readValue(json, Val.class);
    System.out.println(v.getV()); // prints 0
}

我尝试了类似的方法,但到目前为止无法正常工作。

class Val {
    private int v;

    @JsonCreator
    public Val(@JsonProperty("v") String v) {
        System.out.println("in setter");
        try {
           this.v = Integer.parseInt(v);
        } catch (Exception e) {
            this.v = 0;
        }
    }

    public int getV() {
        return v;
    }
}