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

Ser*_*tin 6 spring-mvc optional java-8

@RestController中有代码:

@GetMapping("update_odds")
public Optional<OddsJSON> updateOdds() {
   return eventService.updateOdds();
}
Run Code Online (Sandbox Code Playgroud)

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

{
present: true
}
Run Code Online (Sandbox Code Playgroud)

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

Ser*_*tin 6

只需要在 pom.xml 文件中添加:

<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-jdk8</artifactId>
  <version>2.6.3</version>
</dependency>  
Run Code Online (Sandbox Code Playgroud)