Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video, we'll create a Spring controller that has a method to intercept a URI request, and respond with a simple String message.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Welcome back, and
congrats again on the not so
0:04
convincing success of deploying
your first Java web app.
0:07
Sure, we got an error when we
visited our running application, but
0:11
that was only because our app lacks the
code to handle requests to certain URIs.
0:15
Actually, it lacks the code to
handle requests to any URI.
0:20
So, let's change that.
0:24
What we're going to do is
write a Spring controller.
0:26
In a Spring web application,
0:29
the controller is a Java object whose job
is to handle requests to certain URIs.
0:30
We'll apply annotations to methods
within this class that indicate
0:36
which URI each method should handle.
0:39
First a quick note on URIs and URLs.
0:43
URI stands for
Uniform Resource Identifier, and
0:47
URL stands for Uniform Resource Locator.
0:50
You may have heard the terms
used interchangeably,
0:54
but they're actually different.
0:57
In general a URL is an address that
includes a method for locating a resource,
0:59
with a protocol like HTTP or FTP.
1:04
In addition a URL will include a host
name like teamtreehouse.com, or
1:08
creativecommons.org.
1:13
But a URI doesn't have to.
1:15
Since our application will be coded
without regard to the host name,
1:17
and without indicating the protocol,
I'll continue to use URI.
1:20
All right, with that said, let's code
a controller to handle URI requests.
1:24
Before creating a controller, let's
create a package where we can put them.
1:31
To do this, I'll right click on
calm.teamtreehouse.giflib, and
1:34
choose new package.
1:39
I'll name this package controller.
1:41
And to create the actual controller,
I'll right-click our new package and
1:44
choose New > Java Class.
1:47
Since this will be the controller
that handles all of our requests to
1:50
gif related pages,
I'll name this one gifcontroller.
1:54
The way that we'll indicate that
this class is a spring controller,
1:59
is with the controller annotation
on the class itself, @Controller.
2:02
And that comes from
the spring framework as well.
2:07
Hit enter so that I get the auto
import statement dropped in there.
2:10
In a spring controller you can add as many
methods as you like, though in general
2:15
they should be related to the object the
controller refers to, in our case a gif.
2:19
I'd like to use this controller
to handle requests to
2:24
any page of our application that
displays a list of gifs or a single gif.
2:27
Later on, if we'd like to add a controller
that shows a list of categories, for
2:32
example, we might add
a category controller.
2:36
Let's add a method that will handle
all requests to our home page.
2:40
Since our home page would likely be a list
of the most recent animated gifs that have
2:44
been uploaded, it makes sense to put
that method in our GifController class,
2:48
so I'll add that method now.
2:54
I'm going to call it
public String listGifs.
2:56
I chose to name this method listGifs, but
3:02
it really could be named
anything you like.
3:05
But, how will the Spring framework
know to execute this method
3:08
when a user requests our homepage?
3:11
Now that is accomplished with,
you guessed it another annotation.
3:15
This one is called request mapping, which
refers to the fact that we want to map
3:19
a certain URI request to a Java method.
3:24
The request mapping annotation
has several elements,
3:27
and we'll use just one
of them in this course.
3:30
So I'll start by adding the request
mapping annotation to the method itself
3:33
request mapping, there it is right there,
I'll hit enter on the suggestion.
3:38
So I get that import statement.
3:42
The one element of the annotation
that we'll be using is called value.
3:44
And the value we specify here will be
the URI pattern that we want to map
3:50
to this method.
3:54
In this case, I'll use a single forward
slash indicating that this method will
3:55
handle requests to our application root,
or homepage.
3:59
If you like, since the only annotation
element we use is called value,
4:03
you can omit that name altogether.
4:08
For now, we'll also need to add the
response body annotation to the controller
4:13
method, to indicate that
this string we return should
4:17
be used as the response without
any further processing.
4:20
So, I'll add the responsebody
annotation to the method, as well.
4:23
And, as for the return value,
4:28
I'll choose an arbitrary string to return,
list of all the GIFs!
4:30
All right, we're almost there.
4:38
One more step, though.
4:40
If we tried to use the gradle
boot run task to build and
4:42
deploy our app, we'd find ourselves
left with a bit of disappointment.
4:46
In fact, we'd see the same error
message as before in our browser.
4:50
This is because the Spring framework
doesn't automatically scan your packages
4:54
for controllers.
4:58
You have to instruct
the framework to do so.
4:59
Let's go back to our app config class and
5:03
add the annotation that
accomplishes this task.
5:05
Here we'll add a simple annotation
to tell the spring framework
5:09
to scan the current package for
controllers.
5:12
And this scan we'll include our
sub package name, controller.
5:15
The annotation name is component scan.
5:19
Excellent.
5:22
We're ready to test this thing.
5:23
Again, because we're using gradle,
this is pretty straight forward.
5:25
If you don't already have your server
running, in the gradle tool window you can
5:29
expand tasks, right click boot run and
choose run GifLib.
5:33
Since I still have mine running,
I'm going to click the stop button first.
5:38
After a few moments, you should see a
message in the console here that indicates
5:42
that the process has been stopped.
5:47
There it is, it finished running boot run.
5:49
And in fact, since I just stopped it,
I could either go up here and right-click
5:52
boot run and choose run again, or,
since it's the task that I just stopped,
5:56
this gives me a convenient rerun button,
by clicking this play button right here.
6:00
I'll click that now.
6:06
After a few moments, you should see
some output at the bottom of the screen.
6:09
This output is largely the same as we saw
before, with one important difference.
6:13
If you look closely,
6:19
you'll see a line that says the root path
has been mapped to our list gifs method.
6:20
So if you look here,
6:27
here is the single slash that has
been mapped onto java.lang.string.
6:28
That's the return value
of our controller method.
6:33
And if we scroll over, we see the fully
qualified name of the method,
6:38
meaning package name,
class name, there it is.
6:41
Method name.
6:45
The slash URI that is
the root of our application,
6:46
has been mapped to our list gifs method.
6:50
Excellent.
6:54
Switching over to a browser,
we can again navigate to local host.
6:55
Including the port number 8080 and
see our message so plainly, yet
7:01
so wonderfully on display.
7:06
There it is.
7:08
List of all the gifs.
7:09
Try coding another method and
gif controller, and add a request mapping
7:11
annotation, and the response body
annotation to capture the uri/gif.
7:15
Returning a string message of your choice.
7:22
When you're done writing that method,
7:24
you'll test this in the browser by
stopping and restarting the bootrun task.
7:26
Then coming back here to the browser and
7:31
entering /gif at the end of the URI here.
7:34
When you're done,
you shouldn't see this error message, but
7:41
rather, the custom message that you've
returned in your new controller method.
7:44
Go ahead, give it a shot.
7:49
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up