We will create a simple WelcomeBean POJO (short for Plain Old Java Object) with a member field called message and one argument constructor, as shown in the following line of code:
data class WelcomeBean(val message: String = "")
The corresponding Java class is listed as follows:
public class WelcomeBean {
private String message;
public WelcomeBean(String message) {
super();
this.message = message;
}
public String getMessage() {
return message;
}
}
Kotlin automatically adds constructors and other utility methods to the data classes.
In the previous method, we returned a string. Let's create a method that returns a proper JSON response. Take a look at the following method:
@GetMapping("/welcome-with-object")
fun welcomeWithObject() = WelcomeBean("Hello World")
The method returns a simple WelcomeBean initialized with a "Hello World" message.