Link to home
Start Free TrialLog in
Avatar of dkim18
dkim18

asked on

Javalin coding based on existing skeleton

Hi Experts,


I am working on small Javalin project. 

The ad server will have an API to return ads, and several APIs to configure ads that can be returned from the ad API.



In particular, I need to create:
· an API that will take in and retrieve singular Supply as JSON (see below)
· an API that will take in and retrieve singular Deals as JSON (see below)
· an API that will take in and retrieve singular Tags as JSON (see below)
· an ad API that will return an ad in the specified format below. Ads are requested using a supply ID, and each response should contain a single ad (distinguishable by the Ad id in the response). Any ad that is applied to that Supply can be returned, but if the same supply ID is provided on repeated requests, all eligible Tags should eventually be returned.

The relationships between the configuration objects are as follows:
· Supply MAY have associated Tags
· Deals MAY have associated Tags
· Tags MUST have a Deal
· Tags MAY have associated Supply
· Tag and Supply associations MUST be symmetrical (i.e. if a Tag is associated with a Supply, then that supply MUST be associated with the Tag) 


The uri for ad requests will look like:
            http://localhost:9000/0/ad?supplyId=123

Data Objects
Supply
{
  "id": 123,
  "name": "Supply",
  "tags": [789, 123]
}
Deal
{
  "id": 456,
  "name": "Deal"
}
Tags
{
    "id": 789,
    "name": "Tag",
    "tagUrl": "https://example.com/my-video.mp4",
    "dealId": 456,
    "supplySources": [123, 456]
}
 
Ad Response
<?xml version="1.0" encoding="utf-8"?>
<VAST version="2.0">
        <Ad id="789">
                <InLine>
                        <AdTitle>Tag</AdTitle>
                        <Creatives>
                                <Creative sequence="1">
                                        <Linear>
                                                <MediaFiles>
                                                        <MediaFile delivery="progressive" bitrate="256" width="480" height="352" type="video/mp4">https://example.com/my-video.mp4</MediaFile>
                                                </MediaFiles>
                                        </Linear>
                                </Creative>
                        </Creatives>
                </InLine>
        </Ad>
</VAST>

This basic skeleton of an application using javalin to create the routes for these APIs is given. State does not have to be maintained when the application is restarted. 

package com.abc.javaad;

import io.javalin.Javalin;
import static io.javalin.apibuilder.ApiBuilder.*;

public class Server {
  public static void main(String[] args) {
    Javalin app = Javalin.create().start(9000);
    //app.get("/", ctx ->
    //ctx.html("Hello, Javalin!"));
    
    app.routes(() -> {
      path("0", () -> {
        path("supply", () -> {
          post( ctx -> ctx.result("OK")); // Save a supply
          path( ":id", () -> {
            get( ctx -> ctx.result(ctx.pathParam("id"))); // Retrieve a supply
            put( ctx -> ctx.result(ctx.body())); // Update a supply
          });
        });
        path("deals", () -> {
          post( ctx -> ctx.result("OK")); // Save a Deal
          path( ":id", () -> {
            get( ctx -> ctx.result(ctx.pathParam("id"))); // Retrieve a deal
          });
        });
        path("tags", () -> {
          post( ctx -> ctx.result("OK")); // Save a tag
          path( ":id", () -> {
            get( ctx -> ctx.result(ctx.pathParam("id"))); // Retrieve a tag
            put( ctx -> ctx.result(ctx.body())); // Update a tag
          });
        });
        path("ad", () -> {
          get( ctx -> ctx.result(ctx.queryParam("supplyId"))); // Retrieve an ad
        });
      });
    });
  }
}

Open in new window


I've never coded Javalin before and went through documentations.

I wasn't sure where I should add my code, so I created separate class(AdController).


public class Server {
  public static void main(String[] args) {
...
...
app.get("supply", AdController::getSupply);//save supply
app.get("supply/:id", AdController::retrieveBySupplyId);//retrieve supply by id   app.routes(() -> {       path("0", () -> {         path("supply", () -> {           post( ctx -> ctx.result("OK")); // Save a supply           path( ":id", () -> {             get( ctx -> ctx.result(ctx.pathParam("id"))); // Retrieve a supply             put( ctx -> ctx.result(ctx.body())); // Update a supply           });         }); ... ... ...

Open in new window


public final class AdController {


private AdController(){ }


static String jsonSupply = "{\"id\":123,\"name\":\"Supply\", \"tag\":[789,123]}";

public static void getSupply(Context ctx)throws  JsonParseException, JsonMappingException, IOException {

ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(jsonSupply);
        ctx.json(jsonNode);
}


public static void retrieveBySupplyId(Context ctx) throws JsonMappingException, JsonProcessingException {

String param = ctx.pathParam("id");
        
if(param.toString().equals("123")) {
        ctx.result(ctx.attributeMap().toString());
        }else {
        ctx.result("No result!");
        }
}

Open in new window


Where and how do I add all the implementation that satisfies above spec?

Can somebody give me some examples?


thanks in advance!

Avatar of David Favor
David Favor
Flag of United States of America image

I've written many APIs over the years + manage many high throughput API systems on a daily bases...

Here's a few suggestions...

1) Javalin + any other tech is fine if you expect no/low traffic.

2) If you expect your project to succeed wildly, avoid Java like the plague... instead...

3) Stick with Apache + PHP, as it's unlikely you have the time/budget/will/expertise to duplicate the full features (including caching) of Apache + PHP.
ASKER CERTIFIED SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial