The Java back-end part of the app is independent
of the front-end or the UI. In this case,
the back-end defines endpoint functions and the UI calls
those functions. So we're going to start our explanation of the
code by looking at the definition of the endpoint
functions. So I've opened up HelloWorldEndpoints. It's in, this is
the package and then we've imported the classes that
we need from the spi.config package. SPI basically means endpoint.
So here are the imports that we need to use endpoints. Now here at the top
of the file, it says @Api [UNKNOWN] and
its name equals helloworldendpoints, version equals v1. This
is where we're saying, hey, this file contains
endpoint functions. The description's pretty obvious and then
the client IDs specify which clients are allowed
to use these endpoint functions. Here we specified Constants.WEB_CLIENT_ID,
but actually for now, we don't really care which client uses us. But this one
is important, the API_Explorer. So we've specified
that the API_EXPLORER_CLIENT_ID is allowed to use our
endpoint functions. And if we look at Constants.java, see here,
the WEB_CLIENT_ID, we didn't really set it. But here we did set the
API_EXPLORER_CLIENT_ID. You get this from Constant.API_EXPLORER_CLIENT_ID. And
we get this from the SPI.Constant package. So if you want
API Explorer to be able to access your back-end functions, you do need to enable
the client ID to use API Explorer. Okay, so back here in HelloWorldEndpoints,
we've defined just a couple different endpoints.
They're very, very simple. The first method
is sayHello and we've declared it as an @ApiMethod or an @ApiMethod.
By default, every method inside a file that's been declared
as an @Api is exposed through endpoints. But you can set
the name if you want and the path.
And then the method. So here the sayHello method
simply returns a new instance of the HelloClass
object. We'll look at that in just a second.
And then the next method, sayHelloByName takes an argument,
name, and we've declared it must be named, the
argument must be named name, and we return a
new instance of the HelloClass created with the name
string. That's all these methods do. Basically, they say
hello and they say hello by name. So both
of these methods return an instance of HelloClass. So
let's quickly take a look at HelloClass. And you
see this is a really simple class. Doesn't
import anything. It's just a POJO, plain old Java
object. It has one field message. Has a
default constructor. And it has a constructor that takes
the name and generates a message based on
the name and sets a message. And that's all
it does. Now the reason we need this
class is because endpoint functions can't return string objects.
They have to return objects with fields. Otherwise, we
could probably just define our endpoint functions to return
a string of the message. But since we need
a real object, we're using HelloClass for it. Okay, now
I'm going to run my app. I'm going to run it
on the dev server, on localhost. So this time since
I've already got the configuration, the run configuration setup,
I just go to Run As, and then I choose
Maven build. And I choose the configuration that
I set up earlier and then I choose
OK. Now it going to run it on localhost.
Again, you can go to localhost and see your
app. See the homepage for your app. But
actually, I don't want to do that right now.
I want to go straight to the API Explorer. To get there, you just
add a /_ah/api/explorer.
It's localhost:8080/_ah/api/explorer.
So you see this when you go to the API explorer. You
can click on Services to show the services, and then you can click on
the available API to drill down to see what functions are available.
And we have sayHello and we have sayHelloByName.
And if you recall, these are the two functions
that were defined in HelloWorldEndpoints.java. And you can actually
execute these functions right here in the API Explorer.
So we click sayHello, we say Execute and then
you scroll down and you'll see the request, which
shows the requests that got sent. And you see
the response and you see our response is messages
Hello World. Kind of what we expected. Now let's
go up and try the next one, sayHelloByName. Now, see
here it says name. Remember how we have @name
in our API? Well, this is going to show the name
here, a bit confusing because we have @name and
name, but this is the name argument right here. So
let's enter a name and then we'll execute it.
And again, here is the request, but here is the
response. Hello Lauren. It took the name that we supplied. Okay.
Cool. So now you know how to use the APIs Explorer.