 Hello, Java developers. My name is Matt Raebel. Today I'd like to show you how to build a secure REST API and native image with Micronaut. Let's get it up. I created a demo script for this screencast that you can find in the description below, along with a link to the blog post and a full table of content so you can skip around and see how everything works in this video. And so it is written in ASCII doctors. So I have a nice little plug in here that I can use to get a nice little view. We're just going to build a REST API, secure it with OAuth2, and then access it with a JWT, and then we'll build a native image and make sure all that works fine. And if there's any code that I type a few characters and all of a sudden it spits out a bunch, those are my IntelJ Live templates, which you can find at Emrable Idea Live Templates. And so you will need a few prerequisites. Had those up here. SDKman, which you can get from SDKman.io. So I already have that installed. And then HTTPi, that you can get from HTTPi.org and also the Octa CLI, which I have 0.10. You can get that from CLI.octa.com. And you'll need GraalVM Java 17 version. So I have this shortcut here to install that. I already have it installed. And if I were to run a Java version, there you go, it proves that it's all installed. And so the first thing you wanna do is generate an OAuth2 access token so we can talk to this API. So the Octa CLI makes that easy. Octa apps create spa. And we'll do OIDC debugger as the name. And we're gonna do OIDC debugger slash debug as the redirect URI. The logout one is just fine there. And that'll create a new application for us. And if we were to go to the open IDC debugger website, we can put this on the right. You'll see it already populated, you know, my org information in there. That's cause I did this before. But you'll need to tack on v1 authorize. That's right over here. It tells you to do that. And then make sure and put your client ID in there and select code and use pixie and make sure v1 token is down here in the token URI and then click send request. And then it does a proof key for code exchange and does secure OAuth dance in the browser with authorization code flow and gives us that access token. So now we can close that and go ahead and set that access token in our terminal. And then we can get to building a micronaut Java REST API. So SDK install micronaut. We already have 343 there. And then we can use MN create app and give it your package name, com octa REST app. And we're gonna use Maven since we use Maven with the others. And then security dash JWT as a feature and micronaut AOT as a feature. And then we're just gonna move it from app to micronaut then CD into it and open it up in IntelJ. And then here's one of my shortcuts, MN hello. You can see that gives us a hello controller at the slash hello endpoint. It takes a get request and it is secured and it'll return the principal's name. So now we need to configure octa in application YAML. So we'll just copy and paste this in there right under security here and get our octa domain. Where did we have that? We had it back in our terminal and replace this. Okay, so we have the issuer, we have the signatures there and then we can start up our app. MN run. And then you can see it started up in 283 milliseconds. That time will vary if we try it again. Might be faster this time. It just kind of depends. I mean we're running on the JVM so even starting up in 280 milliseconds is pretty awesome. And then we can hit it with HTTP IE. We can recall that token we set and then try HTTP with that token. And now it works. It lets us in and it prints out our email there. And so now we can build a native app. So we're gonna exit out of this one and control C on that one and go ahead and use maven, clean, package, packaging, native image. And so you might be wondering, why did you use clean there? Well, there's a bug in my cannot three, four, three that requires you to do that after you've run it. And so I believe that'll be fixed in three, four, four. So just something to be aware of. You can see that took about a minute to complete. 58 seconds there. If we wanted to start it up, we could do target app and 164, which is a little long for a native app. Ooh, 16 milliseconds, 16, 16. So that seems to be about the average there. And just to do some startup time comparisons here, this is comparing with other frameworks. What I found on a MacBook Pro Intel version, so top of the line when they used to make Intel or the old Intel line, which was I think 2019 and it's 64 gigs of RAM, pretty nice beefy machine. It'll start up in about 28 milliseconds. You compare that to Quarkus, which is a bit faster, but it's twice as fast as Spring Boot and certainly faster than Helodon, about 25% faster. And so that's just a comparison there. And then for memory used after it starts up, you'll see 31 megs, which is awesome for a Java app, right? And then after five requests, it's up to 56. So you get most of that increase just after the first one. Here's the command I used to see how much memory they're using, not quite as fast as Quarkus or as much less memory used, but certainly the second, right? And very good there. And I encourage you to clone the repo here. Native Java examples, if you wanna test them all out, they're all available right there and there's instructions in the read me for how to do it. And I also did it on the MacBook Pro M1 Max, which I'm using right now. The app start up twice as fast, but they do use some more memory. So I'm not sure why that is, but milliseconds to start for micronaut, 17 milliseconds and you saw, right? In my examples, what were we doing? And then 13 or something like that, 16. So that's about around there. And then megabytes used on start versus after five requests. So we've also built this into the Octa CLI. So now if you were to go to, you know, something like downloads or there's nothing in here, you can do octa start, micronaut, and it'll actually download and configure a micronaut app for you. And we keep this example up to date so you can use it whenever you want. So you can find this example code on native Java examples in the micronaut directory. And of course, there's the blog post about building native Java apps. So I hope you've enjoyed this screencast. You can find me on Twitter at mrable. You can find my team on Twitter at octadev and subscribe to our YouTube channel. Smash that button so you can get notified when there's more new content like this. Cheers, have a great day.