|
1 | | -# Example oFunctions Project: String Reverse |
| 1 | +# Example Java Function: String Reverse |
2 | 2 |
|
3 | | -This example provides an HTTP endpoint for reversing strings |
| 3 | +This example provides an HTTP trigger endpoint for reversing strings. |
4 | 4 |
|
5 | 5 | ```bash |
6 | | -$ curl -d "Hello, World!" "http://localhost:8080/r/string-reverse-app/reverse" |
7 | | -!dlroW ,olleH |
| 6 | +$ curl -d "Hello World" http://localhost:8080/t/string-reverse-app/string-reverse |
| 7 | +dlroW olleH |
8 | 8 | ``` |
9 | 9 |
|
10 | 10 |
|
11 | 11 | ## Demonstrated FDK features |
12 | 12 |
|
13 | | -This example uses **no** features of the fn Java FDK; in fact it doesn't have |
14 | | -a dependency on the fn Java FDK, it just plain old Java code. |
| 13 | +This example uses **none** of the Fn Java FDK features, in fact it doesn't have |
| 14 | +any dependency on the Fn Java FDK. It is just plain old Java code. |
15 | 15 |
|
16 | 16 | ## Step by step |
17 | 17 |
|
18 | | -Ensure you have the functions server running using, this will host your |
19 | | -function and provide the HTTP endpoints that invoke it: |
| 18 | +Ensure you have the Fn server running to host your |
| 19 | +function and provide the HTTP endpoint that invokes it: |
20 | 20 |
|
21 | | -```bash |
| 21 | +(1) Start the server |
| 22 | + |
| 23 | +```sh |
22 | 24 | $ fn start |
23 | 25 | ``` |
24 | 26 |
|
25 | | -Build the function locally |
| 27 | +(2) Create an app for the function |
26 | 28 |
|
27 | | -```bash |
28 | | -$ fn build |
| 29 | +```sh |
| 30 | +$ fn create app string-reverse-app |
29 | 31 | ``` |
30 | 32 |
|
31 | | -Create an app and route to host the function |
| 33 | +(3) Deploy the function to your app from the `string-reverse` directory. |
32 | 34 |
|
33 | | -```bash |
34 | | -$ fn create app string-reverse-app |
35 | | -$ fn create route string-reverse-app /reverse |
| 35 | +```sh |
| 36 | +fn deploy --app string-reverse-app --local |
| 37 | +``` |
| 38 | + |
| 39 | +(4) Invoke the function and reverse the string. |
| 40 | + |
| 41 | +```sh |
| 42 | +echo "Hello World" | fn invoke string-reverse-app string-reverse |
| 43 | +dlroW olleH |
36 | 44 | ``` |
37 | 45 |
|
38 | | -Invoke the function to reverse a string |
| 46 | +(5) Invoke the function using curl and a trigger to reverse a string. |
39 | 47 |
|
40 | 48 | ```bash |
41 | | -$ curl -d "Hello, World!" "http://localhost:8080/r/string-reverse-app/reverse" |
42 | | -!dlroW ,olleH |
| 49 | +$ curl -d "Hello World" http://localhost:8080/t/string-reverse-app/string-reverse |
| 50 | +dlroW olleH |
43 | 51 | ``` |
44 | 52 |
|
45 | 53 |
|
46 | 54 | ## Code walkthrough |
47 | 55 |
|
48 | 56 | The entrypoint to the function is specified in `func.yaml` in the `cmd` key. |
49 | | -It is set this to `com.fnproject.fn.examples.StringReverse::reverse`. The whole class |
| 57 | +It is set this to `com.example.fn.StringReverse::reverse`. The whole class |
50 | 58 | `StringReverse` is shown below: |
51 | 59 |
|
52 | 60 |
|
53 | 61 | ```java |
54 | | -package com.fnproject.fn.examples; |
| 62 | +package com.example.fn; |
55 | 63 |
|
56 | 64 | public class StringReverse { |
57 | 65 | public String reverse(String str) { |
58 | | - StringBuilder builder = new StringBuilder(); |
59 | | - for (int i = str.length() - 1; i >= 0; i--) { |
60 | | - builder.append(str.charAt(i)); |
61 | | - } |
62 | | - return builder.toString(); |
| 66 | + return new StringBuilder(str).reverse().toString(); |
63 | 67 | } |
64 | 68 | } |
65 | 69 | ``` |
66 | 70 |
|
67 | | -As you can see, this is plain java with no references to the fn API. The |
68 | | -fn Java FDK handles the marshalling of the HTTP body into the `str` |
| 71 | +As you can see, this is plain java with no references to the Fn API. The |
| 72 | +Fn Java FDK handles the marshalling of the HTTP body into the `str` |
69 | 73 | parameter as well as the marshalling of the returned reversed string into the HTTP |
70 | 74 | response body (see [Data Binding](/docs/DataBinding.md) for more |
71 | 75 | information on how marshalling is performed). |
|
0 commit comments