English subtitles

← Add New Endpoints Function - Developing Scalable Apps with Java

Get Embed Code
4 Languages

Showing Revision 5 created 05/24/2016 by Udacity Robot.

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