In the previous step, we returned a hard-coded string back.
We mapped GET request to this URI, to this method which was returning a simple string. In this
step, we would actually create an object for hello-world-bean and try to return that back and see what
would happen.
One of the things you need to understand is the fact, there is a lot of things that are happening in
the background.
We used Spring Boot to create the project. Through the spring-boot-starter-projects,
we are using frameworks like Spring, Spring MVC, and a lot of other things.
So, there is a lot of magic which is happening in the background.
Before we get there, we would focus on getting another simple GET request working. So, I would copy this
method.
There would be a compilation error.
Let's not worry about it right now.
What I want to do is to create a GetMapping to hello-world
and this time I would want to create a bean and return it back.
So, instead of creating a simple string, I would want to create a bean and return it back. The URI
I would want to
map is hello-world-bean. We would continue mapping the GetMapping request.
However, instead of returning a string, we want to return a bean back. How do we do that? Let's quickly do that.
So, we want to do
hello-world-bean
and I'll make the change in the method name.
Now, you'd see that the entire thing compiles but instead of creating Hello World, what I would want
to do is, to create a HelloWorld
Bean. One more thing I'd want to do is, to make sure that this method is returning a Hello World Bean.
So, instead of string,
we want to return a
Hello World
Bean. Actually, the HelloWorldBean is not really there.
So, let's go ahead and create that first. How do I create it? Control 1 or Command 1 and create class
HelloWorldBean.
Let's do that.
HelloWorld
Bean.
I'll take the default and click Finish and I would want to create, next,
if you'd see, there is a constructor which is present in here. So, let's go ahead and do a Control 1 and
create constructor
HelloWorld. I'd want to create a HelloWorldBean with a message.
What I want to do with is, this.message = message.
Now, I would get another compilation error.
Now, I press Control 1 and I'm going to create a message in here.
So, we have a private field called message.
We have a Hello Bean accepting that in the constructor and setting it to itself. Let's create a Getter, sorry a setter
quickly.
So, I'll say, Select Setters.
OK.
The easiest way to do that is right-click, Source and you can go and select Generate Getters and Setters.
So, I generated setter and obviously, I would also want to generate a toString().
So, let's generate a toString() as well. Right-click, Source, Generate toString(), I'll say, OK. So, we're creating
a very simple bean, nothing complex in here.
So, we're just creating a simple bean and now our HelloWorldController would compile
fine.
So, what we have done until now is very simple, right. We have defined a simple HelloWorldBean which accepts a message
and we are actually returning it back in the controller method.
Let's start the application up.
Let's go to URI,
hello-world-bean.
You would see that there is an error.
It was an unexpected error.
No converter found for return value of type
HelloWorldBean.
This is one of the important things that you'd need to understand.
Actually, it is not really a easy error to debug and that is the reason why I illustrated this first step.
So, this HelloWorldBean does not have a Getter.
So, if we don't create a Getter, then the automatic conversion will not work.
So, let's go out and create the Getter.
So, right-click, Source, Generate Getters and Setters, and select the Getter for that
and now, you'd see that the Getter is present in here.
Let's quickly restart the application.
And now, if you launched the HelloWorldBean, you would see a JSON message back.
You'd see that message: "Hello World".
You can see that this message is adhering to a JSON structure.
We're calling this GET URI
and the HelloWorldBean is being returned with the value Hello World.
And if you look at the structure, it's actually reflecting this structure in here.
So, it's just returning message and Hello World.
The name of the field here is message.
So, that's exactly what is here
and the text which is being set to the message is being presented as its value
and this coming back as a JSON string. In this step, we added in one more simple method.
However, this is returning a bean back.
Earlier, we returned a simple string back.
Now, we are returning a bean back and this bean is being automatically converted into JSON and returned
back. The first two steps were really simple, right.
We just created a couple of simple methods. In the next step, we will understand what's happening in the
background of these two steps.
How is the request being handled,
who is handling the request,
and also, how is the HelloWorldBean content getting translated to a JSON.
We'll look at all that in the next step.