如何返回Optional< T>作为Spring @RestController中的JSON? [英] How to return Optional<T> as JSON in the Spring @RestController?

查看:217
本文介绍了如何返回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>  

这篇关于如何返回Optional&lt; T&gt;作为Spring @RestController中的JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆