Simple method returning a string

Let's start with creating a simple REST service returning a welcome message:

    @RestController    class BasicController {      @GetMapping("/welcome")      fun welcome() = "Hello World"    }

A comparable Java method is shown as follows. A major difference is how we are able to define a function in one line in Kotlin--fun welcome() = "Hello World":

    @GetMapping("/welcome")    public String welcome() {      return "Hello World";    }

If we run FirstWebServiceWithKotlinApplication.kt as a Kotlin application, it will start up the embedded Tomcat container. We can launch up the URL (http://localhost:8080/welcome) in the browser, as shown in the following screenshot:

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.