Hello World: your first Java Api (with service injection!)

Welcome, .NET developers. Let’s get our hands dirty. Today we’re going to build our first Java “hello world” web API, and we’ll look at very basic service plumbing with Spring Boot. Spring Boot is the magic that makes Java web APIs easy to build and fun to work with.

There’s some great tutorials on this out there. Here’s one, from the IDE I’m using:

https://www.jetbrains.com/help/idea/creating-and-running-your-first-restful-web-service.html

However, one of the easiest ways is to use Spring Boot Initializer: https://start.spring.io/

Spring Initializer is fantastic. Whatever project you want, this sets it up with no fuss– it generates the template and you can open it in your IDE and rock. However, there’s a few choices there, so let’s break it down.

Spring Boot 3.5.3 is the latest stable version of Spring Boot. 3.5.4 is prerelease, and 4.0.0 is a preview. So we’ll stick with 3.5.3 for now. Then there’s the build– Maven or Gradle. I’m not sure at this point, so I’ll stick with Maven. And then there’s language– Java or Kotlin. Kotlin is basically Java but with more modern language syntax. Kotlin runs the same as Java, using Kotlin is kinda like using the new language features in c# and making fun of developers who are stuck on .NET 6. However, since I’m new at this– I’m sticking to Java. We’ll look at Kotlin in the future! Finally, pick Jar packaging and the Java 24 runtime.

For dependencies, pull in Spring Boot DevTools and Spring Web.

Then copy the contents of the ZIP file to your working foler, and open it in the IDE. As an alternative, you can do this in IntelliJ using the Generators feature:

After opening the project, add a new Spring Java Component, and choose Rest Controller.

Then add a new String method to the class, add a @GetMapping annotation to the method, and your code should look like the following. Bam! You’ve got a simple hello world API. Debug the project, and open up https://localhost:8080/hello and you’ll see your code in action. Here’s what your working class should look like:

package com.example.helloworld;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
class HelloController {

@GetMapping("/hello")
public String HelloWorld()
{
return "hi";
}
}

Note that there was no special sauce in getting this wired up like in .NET– no mapping of controllers like you’d do in a startup class. Yeah, you could do this in minimal APIs in .NET, but if you added controller classes with dependencies, there’s a lot more wiring in .NET to do. With spring Boot, it’s all about convention and annotations.

Next, let’s add a simple dependency, so the greeting isn’t hardcoded. Let’s add a simple interface like this:

public interface IGreetingService {
    public String Greet();
}

Next, let’s write a simple service to implement this. Choose Spring Boot Component: Service, and then have the class implement the IGreetingService. It should look like this:

package com.example.helloworld.services;

import org.springframework.stereotype.Service;

@Service
class GreetingService implements IGreetingService {

    @Override
    public String Greet() {
        return "Bonjour Hi";
    }
}

I like the C# syntax better personally…. “:” instead of “implements”. Also note that the @Override isn’t required but is probably considered good practice in most cases.

Next, let’s revisit the HelloController, and add the interface to the contructor as an argument. This is the preferred way to inject dependencies, although you can inject fields as well. Add a constructor and a field like the following:

private final IGreetingService greetingService;

public HelloController(IGreetingService greetingService) {
this.greetingService = greetingService;
}

With that, Spring Boot will magically find the service marked with @Service that implements the interface, and it will be available in the controller. The final controller will look like this:

package com.example.helloworld;

import com.example.helloworld.services.IGreetingService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
class HelloController {

    private final IGreetingService greetingService;

    public HelloController(IGreetingService greetingService) {
        this.greetingService = greetingService;
    }

    @GetMapping("/hello")
    public String HelloWorld()
    {
        return greetingService.Greet();
    }
}

With that, you’ve got a simple Hello World type of API, but demonstrating very simple service injection. Note that we didn’t have to access the web application builder and register services like you have to do in .NET. It’s far simpler. I like it.

Next up, we’ll go deeper into the rabbit hole of Java.

If you’re enjoying this, drop a comment and let me know. Until next time… go break some code. 🙂


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *