Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return Optional<T> as JSON in the Spring @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
}

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

like image 443
Serge Nikitin Avatar asked Oct 11 '16 13:10

Serge Nikitin


Video Answer


1 Answers

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>  
like image 69
Serge Nikitin Avatar answered Oct 03 '22 10:10

Serge Nikitin