troubleshooting Question

Javalin coding based on existing skeleton

Avatar of dkim18
dkim18 asked on
Java App ServersJSON* Javalin
2 Comments1 Solution9 ViewsLast Modified:

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
        });
      });
    });
  }
}


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           });         }); ... ... ...


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!");
        }
}


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

Can somebody give me some examples?


thanks in advance!

ASKER CERTIFIED SOLUTION
David Favor
Fractional CTO
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros