In the previous request, we returned a string as the response. Now, let's create an API that returns a proper JSON response.
To be able to return a JSON response, we would need to create a Plain Old Java Object (POJO) to hold the response structure. We will create a simple POJO WelcomeBean class with a member field called message and one argument constructor, as shown in the following code snippet:
package com.mastering.spring.springboot.bean;
public class WelcomeBean {
private String message;
public WelcomeBean(String message) {
super();
this.message = message;
}
public String getMessage() {
return message;
}
}
Take a look at the following method. It returns a simple WelcomeBean class initialized with a message—"Hello World":
@GetMapping("/welcome-with-object")
public WelcomeBean welcomeWithObject() {
return new WelcomeBean("Hello World");
}