The next behavior we want to create will demonstrate how to parse and respond with JSON (as REST APIs tend to do).
Our goal is simple: A request contains JSON with a name, age, and money. We want to take those 3 fields, add $5000 to money and 10 years to age, and return a nicely formatted JSON response.
Getting Old and Rich(er)
Just like we did before, create a new behavior named AboutBehavior inside the behaviors package. Add the following content:
behaviors/AboutBehavior.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.json.encode.JSONRenderer;
import com.ociweb.pronghorn.network.config.HTTPContentTypeDefaults;
import com.ociweb.pronghorn.pipe.StructuredReader;
public class AboutBehavior implements RestListener {
private HTTPResponseService responseService;
// Our 3 fields:
private int money;
private int age;
private String name;
public AboutBehavior(GreenRuntime runtime) {
responseService = runtime.newCommandChannel().newHTTPResponseService();
}
@Override
public boolean restRequest(HTTPRequestReader request) {
}
}