Using Jackson ObjectMapper with Java 8 Optional values

前端 未结 7 1929
长发绾君心
长发绾君心 2020-12-01 05:03

I was trying to use Jackson to write a class value to JSON that has Optional as fields:

public class Test {
    Optional field = Optional.of(\"         


        
7条回答
  •  广开言路
    2020-12-01 05:33

    You could use jackson-datatype-jdk8 which is described as:

    Support for new JDK8-specific types, such as Optional

    In order to do this:

    • add com.fasterxml.jackson.datatype:jackson-datatype-jdk8 as a dependency
    • register the module with your object mapper: objectMapper.registerModule(new Jdk8Module());

提交回复
热议问题