如何返回Optional< T>作为Spring @RestController中的JSON?

如何返回Optional< T>作为Spring @RestController中的JSON?

问题描述:

@RestController中有代码:

There is the code in the @RestController:

@GetMapping("update_odds")
public Optional<OddsJSON> updateOdds() {
   return eventService.updateOdds();
}

此方法在浏览器中的结果:

Result of this method in the browser:

{
present: true
}

是否可以配置Jackson映射器以输出Optional的值?

Is it possible to configure Jackson mapper to output the value of Optional?

只需将其添加到pom.xml文件中即可:

Just need to add to the pom.xml file:

<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-jdk8</artifactId>
  <version>2.6.3</version>
</dependency>