Spring Boot @RestController Action Returning java.util.Optional

Recently, I wondered what would happen if a Spring Boot RestController returned a java.util.Optional instead of a regular POJO.

I invested 30 minutes of my life to find out, and created an over-engineered example on GitHub.

Here is the controller.

@Slf4j
@RestController
@RequiredArgsConstructor
public class MusicController {

    private final MusicService musicService;

    @GetMapping(path = "/value")
    Album getValue(@RequestParam("isNull") boolean isNull) {
        log.info("Request album value (is null: {})", isNull);
        return musicService.getAlbumAsValue(isNull);
    }

    @GetMapping(path = "/optional")
    Optional<Album> getOptional(@RequestParam("isNull") boolean isNull) {
        log.info("Request album optional (is null: {})", isNull);
        return musicService.getAlbumAsOptional(isNull);
    }
}

The first question I had was if Spring Boot would even start up. You never know. It does, and with this hurdle out of the way, here’s the output of a couple of curl commands.

~ % curl -i 'http://localhost:8080/value?isNull=false'   
HTTP/1.1 200 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sun, 24 Jul 2022 08:24:50 GMT

{"artist":"Insomnium","title":"Winter's Gate","genre":"Melodic Death Metal","year":2016} 

~ % curl -i 'http://localhost:8080/value?isNull=true'    
HTTP/1.1 200 
Content-Length: 0
Date: Sun, 24 Jul 2022 08:24:55 GMT
 
~ % curl -i 'http://localhost:8080/optional?isNull=false'
HTTP/1.1 200 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sun, 24 Jul 2022 08:25:00 GMT

{"artist":"Insomnium","title":"Winter's Gate","genre":"Melodic Death Metal","year":2016} 

~ % curl -i 'http://localhost:8080/optional?isNull=true' 
HTTP/1.1 200 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sun, 24 Jul 2022 08:25:06 GMT

null

Everything works as expected except for the null-case with the Optional. It returns the string “null” instead of nothing.

The moral of the story: Do not return java.util.Optional from a @RestController, or you need to do more work to unpack it.

Thank you for reading.

MacBook Air M1 Apple Silicon Sleep High Power Consumption?

When I first got the MacBook and put it to sleep on battery, as one does, I wondered why the energy store had lost a surprisingly high amount of charge after just a couple of days of sitting on a shelf untouched, lid closed. Now that I have a power meter (almost a year later), I was curious and hooked up the plugged-in computer to it. As you can see from the title image, the result was 2.3 frigging watts. But why?

Given this number, I wanted to open this blog post with the following statements.

Here is an interesting fact for you. The most power-efficient computer of the past decade allows itself over 2W of power while it sleeps. Yes, two frigging watts. Sleeping.

Being a curious nut, I did some more digging and then overhauled this blog post accordingly. But first, let me continue with my original vision of this little rant.

This is time travel, my friends.

Read More »

The Effects of Burnout – Personal Report About Navigating a Crisis

If you are one of the few followers of my blog, then, first of all, thank you. Secondly, maybe you are wondering why I have not been writing about any programming topics lately, given the name of this blog. Perhaps you only started following recently and like the gaming content. Whatever your reason, my main focus has always been on software development topics of any kind, and this element has been lacking for quite some time.

Out of the 11 posts this year (at the time of writing), only five fall into the programming category. And if I’m being honest, I have spread two more extensive topics across those five blog posts to potentially get more clicks – although the separation also makes sense. Before I digress too much, the short version is this: I am actively neglecting the original premise of my blog, yet I still want to produce content. There is a reason, and despite that somewhat lighthearted title, it is a serious topic.

Note: Before I changed the title to what it is now, it was “Yo, CODE-Slinger! You Now a GAME-Slinger? No, I’m Having a Dance With Burnout“

Although this report is based on my personal experience with the subject, it is not about me. Nobody on the Internet is interested in me, and I am not delusional enough to think otherwise. Treat it as a biased case study that I sincerely hope can be a motivation for other people going through a similar thing. The light at the end of the tunnel can be an exit.

The following two sections elaborate a little on The Codeslinger origin story. If you are only interested in the meat, skip to “Dance With the Burnout”.

Read More »

Cyberpunk 2077 Ending Review

When I published my Cyberpunk 2077 review last month, I had not yet finished the game. Based on what I had played until that point, I still felt confident in my opinion – hence the review. I beat the game a couple of weeks later and have watched all possible endings on YouTube (no, I did not play them all myself). My general stance on the game has not changed, but I am even more convinced that Cyberpunk is a character and narrative-driven game, first and foremost.

Before I go on, beware that I use this blog post to talk freely, something I avoid in my usual reviews. I will drop a few spoilers, and although I try to stay as vague as possible, there will be a few hints here and there. With a little more knowledge and research under my belt, I will also briefly return to gameplay and the technical aspects of CD Projekt Red’s ambitious creation.

Read More »