# Saying Hello

A ***Behavior*** in GreenLighting is an actor in the [actor model](https://en.wikipedia.org/wiki/Actor_model). It has a clearly defined and limited responsibility in the bigger scope of a project and can listen and respond to messages. Unlike traditional models, behaviors communicate through these messages with each other.

For our tutorial, we need two behaviors that can listen to HTTP Requests and respond with HTTP content. Let's focus on our GET request first.

## Hello Behavior

Create a new package inside your project named `behaviors` . Add a new class named `HelloBehavior` to the package. Use the code below to get started:

{% code title="behaviors/HelloBehavior.java" %}

```java
package com.ociweb.behaviors;

import com.ociweb.HelloField;
import com.ociweb.gl.api.GreenRuntime;
import com.ociweb.gl.api.HTTPRequestReader;
import com.ociweb.gl.api.HTTPResponseService;
import com.ociweb.gl.api.RestListener;
import com.ociweb.pronghorn.network.config.HTTPContentTypeDefaults;
import com.ociweb.pronghorn.pipe.StructuredReader;

public class HelloBehavior implements RestListener {
    private HTTPResponseService responseService;

    public HelloBehavior(GreenRuntime runtime) {
        responseService = runtime.newCommandChannel().newHTTPResponseService();
    }
    @Override
    public boolean restRequest(HTTPRequestReader request) {
    }
}�
```

{% endcode %}

* The behavior implements the `RestListener` interface. It provides everything we need to respond to an API request.
* We require a `HTTPResponseService` object for outputting a HTTP response. It is created in the constructor using the provided `runtime` object.&#x20;

Next, let's expand the `restRequest` method to actually render a plaintext response back to the browser:

{% code title="behaviors/HelloBehavior.java" %}

```java
@Override
public boolean restRequest(HTTPRequestReader request) {
    if(!request.isVerbGet()) {
        return responseService.publishHTTPResponse(request, 404);
    }        
    StructuredReader record = request.structured();
    String name = record.readText(HelloField.NAME);

    return responseService.publishHTTPResponse(request, 200,
            HTTPContentTypeDefaults.PLAIN, w -> {
                w.append("Hello World, ").append(name).append("!");
            });
}
```

{% endcode %}

* On line 3, we make sure that this is a GET request.
* On line 6, we use a `StructuredReader` to parse the request URL. This maps all the known fields to the fields discovered in the request.
* On line 7, we access the record and read the name field previously registered in our route.
* On line 9, we use the `responseService` mentioned above to publish the HTTP response.&#x20;
  * The status code is 200 (Success).
  * The content type we are sending back is PLAIN (simple text).
  * We use a writer lambda to create our text response. This allows for garbage-free String concatenation. First, we append the phrase "Hello World", then the name, and then an exclamation point.

At this point, we're almost done with our GET request. There's one more thing we need to do: Actually register our brand new behavior.

Open up `RestServer` again and edit the `declareBehavior` method to look like this:

{% code title="RestServer.java" %}

```java
@Override
public void declareBehavior(GreenRuntime runtime) {
    runtime.addRestListener(new HelloBehavior(runtime))
            .includeRoutesByAssoc(HelloStruct.HELLO_WORLD);
}
```

{% endcode %}

�We pass in `runtime` to our new `HelloBehavior` and include the *HELLO\_WORLD* route previously associated with the `/api/hello/${NAME}` path.

**Congratulations**, you created your first GreenLightning API endpoint. If you would like, you can start your server by running `main` inside of `GreenLightning.java` and navigate to the URL output in the console. Use a utility such as [Postman](https://www.getpostman.com/) to test your endpoint.

On the next page, we will take it a step further and add JSON parsing and rendering to the mix.

&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://oci-pronghorn.gitbook.io/greenlightning/chapter-1-getting-started-with-greenlightning/simple-rest-server/behaviors.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
