Return to Video

Add New Endpoints Function - Developing Scalable Apps with Java

  • 0:00 - 0:05
    So let's take a look at the code at this new method, greetByPeriod to
  • 0:05 - 0:08
    application. So, here I'm in Hello world
  • 0:08 - 0:13
    and poin.java, I've defined my new function greetByPeriod.
  • 0:13 - 0:15
    Still got the name argument, and it's
  • 0:15 - 0:19
    got a named period argument, period. And just
  • 0:19 - 0:22
    for variety, although the name is @Named
  • 0:22 - 0:26
    period. I'm calling the argument time of day,
  • 0:26 - 0:32
    just to show that whatever the @Named is what is required to be specified
  • 0:32 - 0:37
    in the request. And I'm all doing here is returning new HelloClass name, time of
  • 0:37 - 0:41
    day. HelloClass class, didn't actually have this
  • 0:41 - 0:44
    constructor, so I added it, it's pretty
  • 0:44 - 0:47
    simple. Here's the new construction HelloClass. Pass
  • 0:47 - 0:51
    the name, the period, construct a new message,
  • 0:51 - 0:55
    good, this would be morning, let's say, good morning
  • 0:55 - 0:58
    Lauren. Good morning Lauren. Okay, so let's see if it
  • 0:58 - 1:00
    works. I'm going to run it on the dev
  • 1:00 - 1:03
    server, save my changes, it's always a good thing to
  • 1:03 - 1:06
    do. Mkay, let's go over to the local host
  • 1:06 - 1:08
    in the browser. And I'm going to go straight to
  • 1:08 - 1:12
    the API's explorer. Oh goodness, got an error, that
  • 1:12 - 1:17
    never happens. Failed to retrieve API configs with status: 500.
  • 1:17 - 1:20
    All right, first instinct panic, second instinct go
  • 1:20 - 1:22
    back and check the code. And you see I
  • 1:22 - 1:26
    got the error showing up here too, Failed
  • 1:26 - 1:29
    to retrieve API configs. That means there is something
  • 1:29 - 1:31
    wrong with my end point function. Let's look
  • 1:31 - 1:36
    at hello world end points greetByPeriod all looks good,
  • 1:36 - 1:39
    what's the problem? Aha, it's a copy and
  • 1:39 - 1:42
    paste error. See here, I copy and pasted this
  • 1:42 - 1:45
    from the previous method sayHelloByName. And I
  • 1:45 - 1:49
    forgot to update the function name. So I'll
  • 1:49 - 1:52
    just fix that real quick. You can't have
  • 1:52 - 1:55
    two functions exposed with the same name. Actually
  • 1:55 - 2:01
    greet by period. Save it. And actually when you make changes in the Java code
  • 2:01 - 2:04
    and you save it. You'll usually find that
  • 2:04 - 2:08
    the changes get automatically propagated out to localhost.
  • 2:08 - 2:11
    See it says, reloading the web application, a
  • 2:11 - 2:14
    file has changed. So I don't need to
  • 2:14 - 2:16
    run it again. I'll just go straight to
  • 2:16 - 2:20
    local host, reload the APIs Explorer. Okay good.
  • 2:20 - 2:25
    This time it worked. Now drill down into my API. Here's my new one, greet by
  • 2:25 - 2:31
    period. Enter a name. And it's the afternoon
  • 2:31 - 2:33
    now. And then let's look at the response.
  • 2:33 - 2:38
    There it is. Good afternoon. And then Good afternoon Buttercup, because
  • 2:38 - 2:42
    Buttercup's the name I gave. So one other thing to look at is the code for
  • 2:42 - 2:48
    index.html and hello.js. Take a quick look
  • 2:48 - 2:54
    at those, index.html. Really what we had to do was add the,
  • 2:54 - 2:59
    the new imput field for period is called Period_Field and
  • 2:59 - 3:04
    the new button for the greeting and in Hello.js.
  • 3:04 - 3:07
    And enable buttons that we needed to enable to
  • 3:07 - 3:09
    function for the new button. Here's the new button,
  • 3:09 - 3:14
    input greetByPeriod. We're going to call the function greetByPeriod. We're going
  • 3:14 - 3:17
    to update the button label. Now we have to
  • 3:17 - 3:19
    define greet by period and here it is, it's
  • 3:19 - 3:22
    very similar to greet by name. But we get
  • 3:22 - 3:24
    the name field, we get the value of the name
  • 3:24 - 3:26
    out of the HTML, we get the period
  • 3:26 - 3:30
    out of the period field element. You construct
  • 3:30 - 3:33
    the request. And here we're calling the greetByPeriod
  • 3:33 - 3:36
    function. And then we're specifying the argument. The
  • 3:36 - 3:40
    arguments are name and period. Now because we've
  • 3:40 - 3:44
    called the period, we've named the period argument,
  • 3:44 - 3:47
    at period. This must exactly match. And then
  • 3:47 - 3:50
    we execute it and we call the sayHelloCallBack
  • 3:50 - 3:54
    as before. And again, it throws up an alert dialog box showing
  • 3:54 - 3:57
    us the message in the response. So, that's all there is to it.
Title:
Add New Endpoints Function - Developing Scalable Apps with Java
Description:

more » « less
Video Language:
English
Team:
Udacity
Project:
UD859 - Developing Scalable Apps
Duration:
03:58

English subtitles

Revisions Compare revisions